在 WordPress 中,使用 WP_Query
进行文章查询是最常见的操作,学习好这方面的操作, WordPress 开发基本就学会了一半。
「WordPress果酱」将通过一系列教程讲解如何使用 WP_Query
进行 WordPress 文章查询。
我写这一系列文章的目的也是为了方便自己使用这些参数的时候方便查询,所以如果你也是经常进行 WordPress 二次开发的话,建议收藏本文。
第一讲,把 WP_Query
最常用的分类,标签和分类模的所有相关的参数讲解一下,例子也是来自官方文档,学完之后,你可以在 WordPress 进行非常复杂的多重筛选。
分类有以下 5 个参数:
使用分类 ID 获取含有某个分类(以及该分类下的所有子分类)的文章:
$query = new WP_Query( array( 'cat' => 4 ) );
使用分类别名获取含有某个分类(以及该分类下的所有子分类)的文章:
$query = new WP_Query( array( 'category_name' => 'php' ) );
使用分类 ID 获取含有某个分类(不含该分类下的所有子分类)的文章:
$query = new WP_Query( array( 'category__in' => 4 ) );
使用分类 ID 获取含有几个分类(只要含有一个)的文章:
$query = new WP_Query( array( 'cat' => '2,6,17,38' ) );
使用分类别名获取含有几个分类(只要含有一个)的文章:
$query = new WP_Query( array( 'category_name' => 'staff,news' ) );
使用分类别名获取含有几个分类(都含)的文章:
$query = new WP_Query( array( 'category_name' => 'staff+news' ) );
使用分类 ID 获取不含有几个分类的文章,在 ID 前面加上-号:
$query = new WP_Query( array( 'cat' => '-12,-34,-56' ) );
使用分类 ID 获取同时含有几个分类的文章:
$query = new WP_Query( array( 'category__and' => array( 2, 6 ) ) );
使用分类 ID 获取含有几个分类(只要含有一个)的文章(注意这些分类的子分类单独关联的文章不会获取):
$query = new WP_Query( array( 'category__in' => array( 2, 6 ) ) );
使用分类 ID 获取不含有几个分类的文章:
$query = new WP_Query( array( 'category__not_in' => array( 2, 6 ) ) );
有以下 7 个参数:
使用标签别名获取含有某个标签的文章:
$query = new WP_Query( array( 'tag' => 'cooking' ) );
使用标签 ID 获取含有某个标签的文章:
$query = new WP_Query( array( 'tag_id' => 13 ) );
使用标签别名获取含有某几个标签的文章:
$query = new WP_Query( array( 'tag' => 'bread,baking' ) );
使用标签别名获取都含有某几个标签的文章:
$query = new WP_Query( array( 'tag' => 'bread+baking+recipe' ) );
使用标签 ID 获取都打了标签 ID 为 37 和 47 的文章:
$query = new WP_Query( array( 'tag__and' => array( 37, 47 ) ) );
使用标签 ID 获取只要打了标签 ID 为 37 或 47 的文章:
$query = new WP_Query( array( 'tag__in' => array( 37, 47 ) ) );
使用标签 ID 获取都没有打了标签 ID 为 37 或 47 的文章:
$query = new WP_Query( array( 'tag__not_in' => array( 37, 47 ) ) );
tag_slug__in
和 tag_slug__and
参数和对应的 tag__in
和 tag__and
, 一样,只是它们不使用标签 ID,而是用标签别名。
tax_query
tax_query 参数是 WordPress 3.1 版本引进的,WordPress 非常复杂的多重筛选就是使用它实现的,我们将通过几个例子讲解怎么实现。
特别注意的是:tax_query
参数是分类模式参数数组的列表,首先我们先了解一下它的参数数组的子参数:
在 people
这个自定义分类模式获取值为 bob
的文章:
$query = new WP_Query(array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'people',
'field' => 'slug',
'terms' => 'bob',
),
),
));
从多个分类模式下获取文章:movie_genre
(电影分类)为 action
(动作片),comedy
(喜剧片)并且 actor
(演员)ID不是 103, 115, 206
的电影。
$query = new WP_Query(array(
'post_type' => 'post',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'movie_genre',
'field' => 'slug',
'terms' => array( 'action', 'comedy' ),
),
array(
'taxonomy' => 'actor',
'field' => 'term_id',
'terms' => array( 103, 115, 206 ),
'operator' => 'NOT IN',
),
),
));
获取在 quotes
分类中的文章或者有 quote
文章格式的文章:
$query = new WP_Query(array(
'post_type' => 'post',
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array( 'quotes' ),
),
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array( 'post-format-quote' ),
),
),
));
tax_query
参数支持嵌套,可以用来创建更加复杂的查询。
比如:获取在 quotes
分类中或者同时有 quote
文章格式和在 wisdom
分类中的文章:
$query = new WP_Query(array(
'post_type' => 'post',
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array( 'quotes' ),
),
array(
'relation' => 'AND',
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array( 'post-format-quote' ),
),
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array( 'wisdom' ),
),
),
),
));