WordPress 文章查询教程1:如何使用分类,标签或其他分类模式
在 WordPress 中,使用 WP_Query 进行文章查询是最常见的操作,学习好这方面的操作, WordPress 开发基本就学会了一半。
第一讲,把 WP_Query 最常用的分类,标签和分类模的所有相关的参数罗列一下。
分类参数
有以下 5 个参数:
- cat (int) – 使用分类 ID。
- category_name (string) – 使用分类别名
- category__and (array) – 使用分类 ID 数组。
- category__in (array) – 使用分类 ID 数组。
- category__not_in (array) – 使用分类 ID 数组。
使用分类 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 个参数:
- tag (string) – 使用标签别名。
- tag_id (int) – 使用标签 ID。
- tag__and (array) – 使用标签 ID 数组。
- tag__in (array) – 使用标签 ID 数组。
- tag__not_in (array) – 使用标签 ID 数组。
- tag_slug__and (array) – 使用标签别名。
- tag_slug__in (array) – 使用标签别名。
使用标签别名获取含有某个标签的文章:
$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 版本引进的,它有以下几个子参数:
relation (string) – 多个内部分类参数数组之间的逻辑关系。 可能的值为「AND」,「OR」,如果只有一个分类参数数组,不要使用。
taxonomy (string) – 分类模式.
field (string) – 如何获取分类模式(Taxonomy term),可能的值有:「term_id」,「name」,「slug」或者「term_taxonomy_id」,默认是「term_id」。
terms (int/string/array) – Taxonomy term(s).
include_children (boolean) – 决定对于层级的分类模式,是否包含子分类,默认是包含。
operator (string) – 用于比较的参数. 可能的值有:「IN」,「NOT IN」,「AND」「EXISTS」和「NOT EXISTS」,默认是「IN」。
特别注意的是:tax_query 参数是分类模式参数数组的数组。
单个分类模式查询
在 people 这个自定义分类模式获取明为 bob 的文章:
$query = new WP_Query(array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'people',
'field' => 'slug',
'terms' => 'bob',
),
),
));
多重分类模式查询
从多个分类模式下获取文章:
$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' ),
),
),
),
));
文章链接:https://www.lilianhua.com/wordpress-article-query-tutorial-1-how-to-use-categories-labels-or-other-classification-patterns.html
English (US)
Español (ES)
Português (PT)
Français (CA)
Español (MX)
Español (VE)
Español (CO)
Español (AR)
Português (BR)
Quechua (PE)
Guaraní (PY)
简体中文 (ZH)
繁體中文 (HK)
日本語 (JP)
한국어 (KR)
हिन्दी (HI)
Pilipino (PH)
ไทย (TH)
Tiếng Việt (VN)
Bahasa Melayu (MY)
Bahasa Indonesia (ID)
বাংলা (BD)
اردو (PK)
සිංහල (LK)
ភាសាខ្មែរ (KH)
English (UK)
Français (FR)
Deutsch (DE)
Italiano (IT)
Русский (RU)
Nederlands (NL)
Türkçe (TR)
Polski (PL)
Svenska (SE)
Norsk (NO)
Dansk (DK)
Suomi (FI)
Ελληνικά (GR)
Čeština (CZ)
Magyar (HU)
Română (RO)
Български (BG)
Српски (RS)
Українська (UA)


