标签: get_posts

  • 如何使用PHP函数get_posts来构建文章列表

    如何使用PHP函数get_posts来构建文章列表

    WordPress的get_posts是一个强大的函数,允许开发人员从WordPress数据库中检索内容。您可以最详细地指定要查找的文章、页面和自定义文章类型,获取自定义结果集,然后像PHP/MySQL忍者一样过滤和排序项目。

    但是,如果您不是PHP专业人士,请不要害怕,有无数的PHP教程可供您观看或阅读并学习该语言。您只需要一点PHP知识即可创建自定义文章列表以显示在您的网站上,因为get_posts函数保留了一组参数,允许构建简单或高级查询。

    使用WordPress的get_posts分为两个步骤:

    • 首先,您必须构建自定义查询。实际上,它看起来不像MySQL查询,而且您不会编写任何SELECT语句。您只需要定义一个参数数组并将其传递给get_posts函数。WordPress将该数组转换为真实且安全的MySQL查询,针对数据库运行它,并返回一个文章数组。
    • 其次,您必须使用foreach循环遍历get_posts返回的结果集。

    话虽如此,在这篇文章中,我们将首先深入探讨上面提到的关键概念,特别是如何get_posts工作、如何构建自定义查询以及如何在前端站点上显示数据。然后,我将提供一个真实示例,其中包含一段代码,您可以在暂存环境中获取、编辑和使用代码片段,以进行测试和开发。

    注意:我们通常区分文章、页面和自定义文章类型。在本文中,我们使用术语“post”来表示常规博客文章以及页面和自定义文章类型。所有这些文章类型都存储在数据库的“wp_posts”表中。文章类型之间的主要区别在于“post_type”字段的值。从开发人员的角度来看,文章、页面和自定义文章类型都是文章

    get_posts 函数简介

    Codex对get_posts函数的描述如下:

    检索最新文章的数组,或与给定条件匹配的文章。

    我们可以这样使用get_posts

    $args = array(
    ‘numberposts’=> 20,
    ‘category’=> 4
    );
    $my_posts = get_posts( $args );
    if( ! empty( $my_posts ) ){
    $output = ‘<ul>’;
    foreach ( $my_posts as $p ){
    $output .= ‘<li><a href=”‘ . get_permalink( $p->ID ) . ‘”>’
    . $p->post_title . ‘</a></li>’;
    }
    $output .= ‘</ul>’;
    }
    $args = array(
    ‘numberposts’=> 20,
    ‘category’=> 4
    );
    $my_posts = get_posts( $args );
    if( ! empty( $my_posts ) ){
    $output = ‘<ul>’;
    foreach ( $my_posts as $p ){
    $output .= ‘<li><a href=”‘ . get_permalink( $p->ID ) . ‘”>’
    . $p->post_title . ‘</a></li>’;
    }
    $output .= ‘</ul>’;
    }
    $args = array(
    'numberposts'=> 20,
    'category'=> 4
    );
    $my_posts = get_posts( $args );
    if( ! empty( $my_posts ) ){
    $output = '<ul>';
    foreach ( $my_posts as $p ){
    $output .= '<li><a href="' . get_permalink( $p->ID ) . '">' 
    . $p->post_title . '</a></li>';
    }
    $output .= '</ul>';
    }

    上面的函数检索指定类别中的最新20篇博文(默认为'post_type'is 'post')并返回一个对象数组$post。您可以遍历数组以在屏幕上显示文章。这很容易,对吧?

    get_posts 用于WP_Query检索文章项目,并且它保留了一个包含相同参数的数组WP_Query(除了少数例外)。所以我们有一个庞大的变量列表,可以用来构建我们的自定义查询。这些参数分为以下15个类别

    • Author参数
    • Category参数
    • Tag参数
    • Taxonomy参数
    • Search参数
    • Post & Page参数
    • Password参数
    • Post Type参数
    • Order & Orderby参数
    • Date参数
    • Custom Field (post meta) 参数
    • Permission参数
    • Mime Type参数
    • Caching参数
    • Return Fields参数

    快速查看上面的列表可以让您了解可以针对WordPress数据库构建和运行的各种自定义查询。因此,让我们更深入地研究查询参数并开始构建我们的文章列表。

    如何使用get_posts构建查询

    每一类参数都与同一条信息相关。例如,我们可以构建一个查询来检索指定作者或排除指定作者的文章,通过ID或nicename定义作者。同样,我们可以构建查询,按类别、标签、分类、日期、自定义字段等获取文章。

    如何使用参数构建简单查询

    许多参数可以以非常相似的方式使用,无论它们属于哪个类别。例如,以下参数允许按文章作者查询数据库:

    • authorint ) – 作者ID
    • author_name字符串)——作者的user_nicename
    • author__inarray ) – 多个作者ID的数组
    • author__not_inarray ) — 要从结果集中排除的多个作者ID的数组

    我们如何使用这些参数?

    在以下示例中,参数'author' 指定我们想要ID = 1的作者最近撰写的博客文章:

    $my_posts = get_posts( array( ‘author’ => 1 ) );
    $my_posts = get_posts( array( ‘author’ => 1 ) );
    $my_posts = get_posts( array( 'author' => 1 ) );

    相同的 ‘author’参数允许以不同的方式查询数据库:

    // return an array of posts from specific authors
    $my_posts = get_posts( array( ‘author’ => ‘1,5,12’ ) );
    // return an array of posts from specific authors
    $my_posts = get_posts( array( ‘author’ => ‘1,5,12’ ) );
    // return an array of posts from specific authors
    $my_posts = get_posts( array( 'author' => '1,5,12' ) );
    // return an array of posts excluding the specified author
    $my_posts = get_posts( array( ‘author’ => -1 ) );
    // return an array of posts excluding the specified author
    $my_posts = get_posts( array( ‘author’ => -1 ) );
    // return an array of posts excluding the specified author
    $my_posts = get_posts( array( 'author' => -1 ) );

    因此,根据参数的值,您将获得一个结果集,其中包含来自单个作者(整数)、来自多个作者(逗号分隔值列表)或不包括作者(负值)的文章。

    其他参数提供了额外的灵活性。例如,以下调用get_posts返回多个作者的最新博客文章数组:

    // return an array of posts from multiple authors
    $my_posts = get_posts( array( ‘author__in’ => array( 1, 5, 12 ) ) );
    // return an array of posts from multiple authors
    $my_posts = get_posts( array( ‘author__in’ => array( 1, 5, 12 ) ) );
    // return an array of posts from multiple authors
    $my_posts = get_posts( array( 'author__in' => array( 1, 5, 12 ) ) );

    我们还可以排除多个作者:

    // return an array of posts from multiple authors
    $my_posts = get_posts( array( ‘author__not_in’ => array( 1, 5, 12 ) ) );
    // return an array of posts from multiple authors
    $my_posts = get_posts( array( ‘author__not_in’ => array( 1, 5, 12 ) ) );
    // return an array of posts from multiple authors
    $my_posts = get_posts( array( 'author__not_in' => array( 1, 5, 12 ) ) );

    同样,我们可以使用类别参数、标签参数、文章类型参数,但有一些具体的区别。例如,请参见类别参数:

    • cat整数
    • category_name字符串
    • category__and数组
    • category__in数组
    • category__not_in数组

    无论如何,并非所有参数都像这些参数一样易于使用。此外,我们可以在单个查询中使用类别参数、文章类型参数、mime类型参数等。这意味着我们可以对结果集中的项目进行精细控制,并且可以完全基于文章类型、自定义分类法和自定义字段构建更高级的查询。

    所以,让我们更深入地研究吧!

    如何在WordPress中构建高级查询

    让我们通过基于自定义文章类型和自定义分类的更高级查询向前迈出一步。假设您有以下文章类型:

    名称:书籍

    分类名称:book_category、book_author

    支持:标题、编辑器、缩略图、摘录、自定义字段

    自定义文章类型和自定义分类法

    假设您想要指定自定义分类book_category中最新书籍的列表。这是参数数组:

    $args = array(
    ‘post_type’=> ‘book’,
    ‘tax_query’=> array(
    array(
    ‘taxonomy’=> ‘book_category’,
    ‘field’=> ‘slug’,
    ‘terms’=> ‘sci-fi’
    )
    ),
    );
    $args = array(
    ‘post_type’=> ‘book’,
    ‘tax_query’=> array(
    array(
    ‘taxonomy’=> ‘book_category’,
    ‘field’=> ‘slug’,
    ‘terms’=> ‘sci-fi’
    )
    ),
    );
    $args = array(
    'post_type'=> 'book',
    'tax_query'=> array(
    array(
    'taxonomy'=> 'book_category',
    'field'=> 'slug',
    'terms'=> 'sci-fi'
    )
    ),
    );

    上面的参数只是告诉WordPress检索'sci-fi' 'book_category'.

    参数采用'tax_query'参数数组的数组(即数组数组)。这些嵌套数组允许基于多种分类构建非常复杂的查询,如下例所示:

    $args = array(
    ‘numberposts’=> 10,
    ‘post_type’=> ‘book’,
    ‘relation’=> ‘AND’,
    ‘tax_query’=> array(
    array(
    ‘taxonomy’=> ‘book_category’,
    ‘field’=> ‘slug’,
    ‘terms’=> ‘sci-fi’
    ),
    array(
    ‘taxonomy’=> ‘book_author’,
    ‘field’=> ‘term_id’,
    ‘terms’=> 22
    )
    )
    );
    $args = array(
    ‘numberposts’=> 10,
    ‘post_type’=> ‘book’,
    ‘relation’=> ‘AND’,
    ‘tax_query’=> array(
    array(
    ‘taxonomy’=> ‘book_category’,
    ‘field’=> ‘slug’,
    ‘terms’=> ‘sci-fi’
    ),
    array(
    ‘taxonomy’=> ‘book_author’,
    ‘field’=> ‘term_id’,
    ‘terms’=> 22
    )
    )
    );
    $args = array(
    'numberposts'=> 10,
    'post_type'=> 'book',
    'relation'=> 'AND',
    'tax_query'=> array(
    array(
    'taxonomy'=> 'book_category',
    'field'=> 'slug',
    'terms'=> 'sci-fi'
    ),
    array(
    'taxonomy'=> 'book_author',
    'field'=> 'term_id',
    'terms'=> 22
    )
    )
    );

    这些参数允许我们检索由ID #22编写的最新10个'book'文章类型的列表。该参数设置 中列出的每个分类之间的逻辑关系。上面我们将其值设置为,因为我们需要检索属于作者 #22 所写类别的所有书籍。AND

    这些参数允许我们检索由ID为#22的'book_author'编写的'sci-fi''book_category'中最新10种“book”文章类型的列表。'relation'参数设置'tax_query'中列出的每个分类法之间的逻辑关系。在上面,我们将其值设置为AND,因为我们需要检索属于 'sci-fi'类别AND 由作者#22撰写的所有书籍。

     

    如何使用自定义字段参数构建元查询

    有时,您可能需要根据特定的自定义字段键和/或值构建文章列表。

    $args = array(
    ‘meta_key’=> ‘cover’,
    ‘meta_value’=> ‘paperback’,
    ‘meta_compare’=> ‘=’
    );
    $args = array(
    ‘meta_key’=> ‘cover’,
    ‘meta_value’=> ‘paperback’,
    ‘meta_compare’=> ‘=’
    );
    $args = array(
    'meta_key'=> 'cover',
    'meta_value'=> 'paperback',
    'meta_compare'=> '='
    );

    这些参数允许我们通过自定义字段键和值检索所有文章。'meta_compare'设置测试'meta_value'参数值所需的运算符。这里的'meta_value''=',这也是默认值。

    可用值为'=''!=''>''>=''<''<=''LIKE''NOT LIKE''IN''NOT IN''BETWEEN''NOT BETWEEN''NOT EXISTS''REGEXP','NOT REGEXP''RLIKE'.

    这是一个非常简单的示例,但我们可以构建更高级的查询。在下一个示例中,我们在数据库中查询2010年之后出版的奇幻书籍: