WordPress自定义查询WP_Query参数使用方法大全

追格官方小助手/ 2022年07月13日/ WordPress/ 浏览 1935


关于在WordPress中,使用WP_Query进行文章查询的话题,已聊了很多了,本文略作小结,以方便大家。


WP_Query相关的文章汇总如下:


1. WordPress函数:WP_Query的基础用法简介

2. WordPress函数:使用 WP_Query 按作者查询文章

3. WordPress函数:使用 WP_Query 按文章分类查询文章

4. WordPress函数:使用 WP_Query 按文章标签查询文章

5. WordPress函数:WP_Query 使用 tax_query 根据自定义文章分类查询文章

6. WordPress函数:WP_Query 使用 s 进行文章搜索

7. WordPress函数:WP_Query 按文章、页面的ID查询文章

8. WordPress函数:WP_Query 和密码保护文章相关的查询操作

9. WordPress函数:WP_Query 使用参数 post_type 按文章类型搜索文章

10. WordPress函数:WP_Query 根据文章状态查询文章

11. WordPress函数:WP_Query 按评论数量查询文章

12. WordPress函数:WP_Query 如何进行分页查询

13. WordPress函数:WP_Query 查询置顶文章的方法和建议

14. WordPress函数:WP_Query 如何对查询结果排序

15. WordPress函数:WP_Query 如何按照时间查询文章

16. WordPress函数:WP_Query 按用户权限查询文章

17. WordPress函数:WP_Query 如何搜索媒体库(附件)

18. WordPress函数:WP_Query 缓存相关参数说明


WP_Query的全部参数,汇总如下:


$args = [
    // 按作者查询
    'author' => 123,
    'author_name' => 'zhuige.com',
    'author' => '2,6,17,38',
    'author' => -12,
    'author__in' => array(2, 6),
    'author__not_in' => array(2, 6),


    // 按文章分类查询
    'cat' => 4,
    'category_name' => 'zhuige',
    'category__in' => 4,
    'cat' => '2,6,17,38',
    'category_name' => 'zhuige,jiangqie',
    'category_name' => 'zhuige+jiangqie',
    'cat' => '-12,-34,-56',
    'category__and' => array(2, 6),
    'category__in' => array(2, 6),
    'category__not_in' => array(2, 6),


    // 按文章标签查询
    'tag' => 'cooking',
    'tag_id' => 13,
    'tag' => 'bread,baking',
    'tag' => 'bread+baking+recipe',
    'tag__and' => array(37, 47),
    'tag__in' => array(37, 47),
    'tag__not_in' => array(37, 47),


    // 按文章自定义分类查询
    'tax_query' => array(
        array(
            'taxonomy' => 'people',
            'field'    => 'slug',
            'terms'    => 'bob',
        ),
    ),
    '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',
        ),
    ),
    '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' => 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'),
            ),
        ),
    ),


    // 搜索文章
    's' => 'zhuige',
    's' => 'zhuige -jiangqie',


    // 按文章ID或slug查询
    'p' => 7,
    'page_id' => 7,
    'name' => 'about-my-life',
    'pagename' => 'contact',
    'pagename' => 'contact_us/canada',
    'post_parent' => 93,
    'post_parent' => 0,
    'post_parent__in' => array(2, 5, 12, 14, 20),
    'post__in' => array(2, 5, 12, 14, 20),
    'post__not_in' => array(2, 5, 12, 14, 20),


    // 按文章是否密码保护查询
    'has_password' => true,
    'has_password' => null,
    'post_password' => 'zxcvbn',


    // 按文章类型查询
    'post_type' => 'page',
    'post_type' => 'any',
    'post_type' => array('post', 'page', 'movie', 'book'),


    // 按文章状态查询
    'post_status' => 'draft',
    'post_status' => array('pending', 'draft', 'future'),


    // 按文章评论查询
    'comment_count' => 20,
    'comment_count' => array(
        'value' => 25,
        'compare' => '>=',
    ),


    // 文章查询分页
    'posts_per_page' => -1,
    'nopaging' => true,
    'posts_per_page' => 5, 'offset' => 3,
    'paged' => 6,
    'ignore_sticky_posts' => 1,


    // 文章查询排序
    'orderby' => 'title',
    'orderby' => 'menu_order title',
    'orderby' => 'rand',
    'orderby'  => array('meta_value_num' => 'DESC', 'title' => 'ASC'),
    'order'   => 'DESC',


    // 按文章日期查询
    // $query = new WP_Query( 'year=2012&monthnum=12&day=12' );
    'date_query' => array(
        array(
            'year'  => 2012,
            'month' => 12,
            'day'   => 12,
        ),
    ),
    'date_query' => array(
        array(
            'hour'      => 9,
            'compare'   => '>=',
        ),
        array(
            'hour'      => 17,
            'compare'   => '<=',
        ),
        array(
            'dayofweek' => array(2, 6),
            'compare'   => 'BETWEEN',
        ),
    ),
    'date_query' => array(
        array(
            'column' => 'post_date_gmt',
            'before' => '1 year ago',
        ),
        array(
            'column' => 'post_modified_gmt',
            'after'  => '1 month ago',
        ),
    ),


    // 按文章扩展属性查询
    'meta_key'   => 'color',
    'meta_value' => 'blue',

    'meta_key'     => 'color',
    'meta_value'   => 'blue',
    'meta_compare' => '!=',

    'post_type'    => 'event',
    'meta_key'     => 'event_date',
    'meta_value'   => date("Ymd"),
    'meta_compare' => '>',

    'meta_key'     => 'price',
    'meta_value'   => '22',
    'meta_compare' => '<=',
    'post_type'    => 'product',

    'meta_query' => array(
        array(
            'key'     => 'color',
            'value'   => 'blue',
            'compare' => 'NOT LIKE',
        ),
    ),
    'meta_query' => array(
        array(
            'key'     => 'color',
            'value'   => 'blue',
            'compare' => 'NOT LIKE',
        ),
        array(
            'key' => 'price',
            'value'   => array(20, 100),
            'type'    => 'numeric',
            'compare' => 'BETWEEN',
        ),
    ),
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key'     => 'color',
            'value'   => 'blue',
            'compare' => 'NOT LIKE',
        ),
        array(
            'key'     => 'price',
            'value'   => array(20, 100),
            'type'    => 'numeric',
            'compare' => 'BETWEEN',
        ),
    ),
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key'     => 'color',
            'value'   => 'orange',
            'compare' => '=',
        ),
        array(
            'relation' => 'AND',
            array(
                'key' => 'color',
                'value' => 'red',
                'compare' => '=',
            ),
            array(
                'key' => 'size',
                'value' => 'small',
                'compare' => '=',
            ),
        ),
    ),

    
    // 按文章权限查询
    'perm'        => 'readable',


    // 按附件类型查询
    'post_type'  => 'attachment',
    'post_status'    => 'inherit',
    'post_mime_type' => 'image/gif',

    'post_type'         => 'attachment',
    'post_status'       => 'inherit',
    'post_mime_type'    => array('image/jpeg', 'image/gif', 'image/png', 'image/bmp', 'image/tiff', 'image/x-icon'),

    
    // 设置是否启用文章缓存
    'cache_results'  => false,
    'update_post_meta_cache' => false,
    'update_post_term_cache' => false,
];

发表评论

暂无评论,抢个沙发...

客服 工单