<?php
//wp_Query 查询工具
//分类查询
$args = array(
'post_type'=> 'post',
'cat' => 10 //根据分类id查询 子级分类也查询出来
'category_name' => 'cat-test-01,cat_test-02' //获取 多个分类目录下的所有文章,
'category_name' => 'cat-test-01+cat_test-02' //获取 多个分类目录下的共有的文章, 两个分类都有的文章
'category__in' => array(8) 不包含分类下的子分类的文章内容 而 'cat' =>8;是包含子分类的
'category__and' =>array(203,204) 通过分类id 获取共同的文章
'category__not_in' => array(202) 排除的id分类
//标签查询
$arg = array(
'post' => 'post',
'tag' => 'caption,chat', //通过别名获取标签下的内容所有内容,
'tag' => 'caption + chat',//根据标签别名来获取,加号的意思是 获取公共的内容 可以一直加
'tag_id' => '79' //79是标签的id编号 只能获取一个id 不能多个
'tag_id' => array(75,89), //获取多个标签的id,来获取更多文章
'tag_in' => array(75,89), //获取多个标签的id,来获取文章
'tag_and' => array(75,89), //获取多个标签的id,来获取共有的文章
'tag__not_in' => array(75), // 不包含在里面
)
)
//tax_query 的最原始类来查询 各种 分类下的文章
$arg =array(
'post_type' => 'post',
// 1. 按别名
'tax_query' => array(
array(
'taxonomy' => 'category', //分类的类型
'field' => 'sulg', //规定根据别名查询
'terms' => array('anchat') //查询具体的别名
)
)
// 2. 按分类id 'term_id'
'tax_query' => array(
array(
'taxonomy' => 'category', //分类的类型
'field' => 'term_id', //规定根据term_id分类查询
'terms' => array('12') //通过分类id编号查询
)
)
// 3. 按分类别名 带子分类或不带子分类
'tax_query' => array(
array(
'taxonomy' => 'category', //分类的类型
'field' => 'slug', //规定根据别名查询
'terms' => array('aciform') //通过别名获取文章
'include_children' => true // include_children true是默认 包含子分类的 全部查出来, 如果是false 不查询子分类
)
)
// 4. 按分类别名
'tax_query' => array(
array(
'taxonomy' => 'category', //分类的类型
'field' => 'slug', //规定根据别名查询
'terms' => array('aciform','cat-test-02') //通过别名获取文章
'operator' => 'and' // operator的值如果是in 是所有分类的文章, and是分类共有的文章 'NOT IN'是除了的意思
)
)
// 4. 从多个不同的分类类型项目中获取公共的所属文章
'tax_query' => array(
'relation' => 'and',//还有 or 意思是获取公共还是所有
array(
'taxonomy' => 'category', //分类的类型 是 category
'field' => 'slug', //规定根据别名查询
'terms' => array('cat-test-02') //通过别名获取文章
),
array(
'taxonomy' => 'post_tag', //分类的类型 是 post_tag
'field' => 'slug', //规定根据别名查询
'terms' => array('aciform') //通过别名获取文章
)
)
)