精通WordPress get_posts函数构建高级文章列表教程
WordPress的get_posts函数是一个功能强大的工具,能够帮助开发者从数据库中高效检索各类内容。无论是精细的文章、页面还是自定义文章类型,get_posts都提供了灵活的参数设置,让开发者可以像操控PHP/MySQL一样精准地过滤和排序结果集。虽然您可能不是专业的PHP程序员,但无需担心,丰富的PHP教程将助您掌握这项技能。只需掌握基础的PHP知识,就能利用get_posts创建个性化的文章列表,因为该函数内置了丰富的参数选项,支持从简单到复杂的各类查询构建。
使用WordPress的get_posts主要分为两个步骤:首先构建自定义查询,然后遍历查询结果。构建查询时,您无需编写复杂的MySQL SELECT语句,只需定义一个参数数组并传递给get_posts函数,WordPress会自动将其转换为安全的MySQL查询并执行,返回文章数组。其次,通过foreach循环遍历这些数据,在前端展示出来。本文将深入探讨get_posts的工作原理、查询构建方法以及数据展示技巧,并附真实示例供您参考。
get_posts函数简介
Codex对get_posts函数的描述为:检索最新文章的数组,或与给定条件匹配的文章。例如,以下代码将获取指定类别的最新20篇文章:
“`php
$args = array( ‘numberposts’ => 20, ‘category’ => 4 );
$my_posts = get_posts( $args );
if( ! empty( $my_posts ) ){
$output = ”;
foreach ( $my_posts as $p ){
$output .= ” . $p->post_title . ”;
}
$output .= ”;
}
“`
这段代码会返回一个对象数组$post,包含指定类别中的最新20篇文章。您可以通过遍历数组在页面上展示这些文章。get_posts与WP_Query共享大部分参数,但存在一些差异。get_posts默认返回5篇文章,而WP_Query默认返回每页的文章数。您可以通过设置’numberposts’或’posts_per_page’参数来调整返回数量。
get_posts参数分类
get_posts支持多种参数,分为15个类别:
– Author参数:如author、author_name等,用于按作者筛选文章
– Category参数:如cat、category_name等,用于按类别筛选
– Tag参数:用于按标签筛选
– Taxonomy参数:用于按分类法筛选
– Search参数:用于全文搜索
– Post & Page参数:如post_status、post_type等
– Password参数:用于筛选密码保护的文章
– Post Type参数:用于指定文章类型
– Order & Orderby参数:用于排序
– Date参数:用于按日期筛选
– Custom Field (post meta)参数:用于按自定义字段筛选
– Permission参数:用于权限控制
– Mime Type参数:用于按文件类型筛选
– Caching参数:用于缓存控制
– Return Fields参数:用于指定返回的字段
如何使用get_posts构建查询
各类参数通常用于相似的场景。例如,author参数可以按作者ID、用户昵称或排除特定作者进行查询:
“`php
$my_posts = get_posts( array( ‘author’ => 1 ) ); // 单个作者
$my_posts = get_posts( array( ‘author__in’ => array( 1, 5, 12 ) ) ); // 多个作者
$my_posts = get_posts( array( ‘author__not_in’ => array( 1, 5, 12 ) ) ); // 排除特定作者
“`
同样,category、tag和post_type参数也支持类似用法,允许您灵活构建查询。
如何在WordPress中构建高级查询
通过自定义文章类型和分类法,可以构建更复杂的查询。例如,检索’sci-fi’类别的最新书籍:
“`php
$args = array(
‘post_type’ => ‘book’,
‘tax_query’ => array(
array(
‘taxonomy’ => ‘book_category’,
‘field’ => ‘slug’,
‘terms’ => ‘sci-fi’
)
)
);
“`
更复杂的查询可以包含多个分类法,并设置逻辑关系:
“`php
$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
)
)
);
“`
这里的’relation’参数设置为’AND’,表示需要同时满足两个条件。
如何使用自定义字段参数构建元查询
通过meta_key、meta_value和meta_compare参数,可以按自定义字段筛选文章:
“`php
$args = array(
‘meta_key’ => ‘cover’,
‘meta_value’ => ‘paperback’,
‘meta_compare’ => ‘=’
);
“`
更复杂的元查询可以包含多个条件:
“`php
$args = array(
‘post_type’ => ‘book’,
‘tax_query’ => array(
array(
‘taxonomy’ => ‘book_category’,
‘field’ => ‘slug’,
‘terms’ => ‘fantasy’
)
),
‘meta_query’ => array(
‘relation’ => ‘AND’,
array(
‘key’ => ‘year_published’,
‘value’ => 2010,
‘type’ => ‘numeric’,
‘compare’ => ‘>’
),
array(
‘key’ => ‘price’,
‘value’ => array( 10, 25 ),
‘type’ => ‘numeric’,
‘compare’ => ‘BETWEEN’
)
)
);
“`
这里的’meta_query’参数与’tax_query’类似,支持多个条件组合。
为什么get_posts默认返回5篇文章
get_posts与WP_Query共享部分参数,但存在一些差异。默认情况下,get_posts返回5篇文章,而WP_Query返回每页的文章数。您可以通过设置’numberposts’或’posts_per_page’参数来调整返回数量。此外,get_posts还有一些特定参数,如’category’、’include’、’exclude’和’suppress_filters’。
项目排序
orderby和order参数用于对结果集进行排序,支持多种排序方式:
“`php
$args = array(
‘author’ => ‘1,5,12’,
‘orderby’ => ‘name’,
‘order’ => ‘ASC’
);
“`
WordPress 4.0及更高版本支持按元查询排序:
“`php
$args = array(
‘meta_query’ => array(
‘relation’ => ‘AND’,
‘year_clause’ => array(
‘key’ => ‘year_published’,
‘value’ => 2010,
‘type’ => ‘numeric’,
‘compare’ => ‘>’
),
‘price_clause’ => array(
‘key’ => ‘price’,
‘value’ => array( 10, 25 ),
‘type’ => ‘numeric’,
‘compare’ => ‘BETWEEN’
)
),
‘orderby’ => ‘price_clause’,
);
“`
更复杂的排序可以包含多个元查询条件:
“`php
$args = array(
‘meta_query’ => array(
‘relation’ => ‘AND’,
‘year_clause’ => array(
‘key’ => ‘year_published’,
‘value’ => 2010,
‘type’ => ‘numeric’,
‘compare’ => ‘>’
),
‘price_clause’ => array(
‘key’ => ‘price’,
‘value’ => array( 10, 25 ),
‘type’ => ‘numeric’,
‘compare’ => ‘BETWEEN’
)
),
‘orderby’ => array(
‘price_clause’ => ‘ASC’,
‘year_clause’ => ‘DESC’
),
);
“`
如何显示get_posts返回的数据
get_posts返回一个WP_Post对象数组,包含文章的多个属性:ID、post_author、post_name、post_type、post_title等。您可以通过foreach循环遍历这些数据:
“`php
$custom_posts = get_posts( $args );
if( ! empty( $custom_posts ) ){
$output = ”;
foreach ( $custom_posts as $p ){
$output .= ” . $p->post_title . ”;
}
$output .= ”;
}
return $output ?? ‘Sorry. No posts matching your criteria!’;’
“`
在遍历过程中,您可以使用get_permalink函数获取文章链接,因为WP_Post对象没有直接提供URL属性。
在页面上显示文章列表的方法
您可以通过多种方式在页面上展示文章列表:
1. 编辑子主题的页面模板
2. 在侧边栏小工具中添加
3. 使用自定义短代码在文章内容中展示
真实示例:如何使用短码显示自定义项目列表
以下是一个简单的短代码示例,用于在内容中展示自定义文章列表:
1. 在本地WordPress安装的wp-content/plugins文件夹中创建新目录,如wbolt-shortcodes
2. 在该目录中创建同名的.php文件:wbolt-shortcodes.php
3. 在文件中添加以下标题:
“`php
‘post’,
‘numberposts’ => -1
);
$custom_posts = get_posts( $args );
if( ! empty( $custom_posts ) ){
$output = ‘
- ‘;
- ID ) . ‘”>’ . $p->post_title . ‘
foreach ( $custom_posts as $p ){
$output .= ‘
‘;
}
$output .= ‘
‘;
} else {
$output = ‘Sorry. No posts found!’;
}
return $output;
}
add_shortcode( ‘custom_posts’, ‘wbolt_shortcode_custom_posts’ );
“`
使用方法:在文章或页面中插入短代码[custom_posts],即可展示所有文章列表。