1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
// 根据分类方式别名查询
$query = new WP_Query([
'post_type' => 'post',
'tax_query' => [
[
'taxonomy' => 'category', //告诉查询工具,需要查询哪个分类方式
'field' => 'slug', //告诉查询工具,根据什么字段查询数据
'terms' => ['book'], //告诉查询工具,上面指定的字段的值是什么
'include_children' => true, //是否包含子分类。默认值:true
]
]
]);
// 根据分类方式ID编号查询
$query = new WP_Query([
'post_type' => 'post',
'tax_query' => [
[
'taxonomy' => 'category', //告诉查询工具,需要查询哪个分类方式
'field' => 'term_id', //告诉查询工具,根据什么字段查询数据
'terms' => [20], //告诉查询工具,上面指定的字段的值是什么
]
]
]);
// 根据分类方式别名查询,但不包含子分类
$query = new WP_Query([
'post_type' => 'post',
'tax_query' => [
[
'taxonomy' => 'category', //告诉查询工具,需要查询哪个分类方式
'field' => 'slug', //告诉查询工具,根据什么字段查询数据
'terms' => ['book'], //告诉查询工具,上面指定的字段的值是什么
'include_children' => false, //是否包含子分类。默认值:true
]
]
]);
// 从多个分类项目中获取数据
$query = new WP_Query([
'post_type' => 'post',
'tax_query' => [
[
'taxonomy' => 'category', //告诉查询工具,需要查询哪个分类方式
'field' => 'slug', //告诉查询工具,根据什么字段查询数据
'terms' => ['book', 'pancel'], //告诉查询工具,上面指定的字段的值是什么
'operator' => 'AND', //操作类型。默认值是IN。AND表示是交集,NOT IN表示排除
]
]
]);
// 从多个分类方式的分类项目中获取数据
$query = new WP_Query([
'post_type' => 'post',
'tax_query' => [
'relation' => 'AND', //多个分类方式的关系。可用值为AND,OR
[
'taxonomy' => 'category', //告诉查询工具,需要查询哪个分类方式
'field' => 'slug', //告诉查询工具,根据什么字段查询数据
'terms' => ['book', 'pancel'], //告诉查询工具,上面指定的字段的值是什么
'operator' => 'AND', //操作类型。默认值是IN。AND表示是交集,NOT IN表示排除
],
[
'taxonomy' => 'post_tag', //告诉查询工具,需要查询哪个分类方式
'field' => 'slug', //告诉查询工具,根据什么字段查询数据
'terms' => ['test', 'default'], //告诉查询工具,上面指定的字段的值是什么
'operator' => 'AND', //操作类型。默认值是IN。AND表示是交集,NOT IN表示排除
]
]
]);
// 复杂查询,带嵌套。
$query = new WP_Query([
'post_type' => 'post',
'tax_query' => [
'relation' => 'AND',
[
'relation' => 'AND',
[
'taxonomy' => 'category',
'field' => 'slug',
'terms' => ['book', 'pancel'],
],
[
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => ['test', 'default'],
]
],
[
'taxonomy' => 'post_copy',
'field' => 'slug',
'terms' => ['test', 'default'],
]
]
]);
|