WP_Query是wordpress提供的一个类,它支持的参数非常完善灵活,通过WP_Query类可以创建自己所需要的文档循环输出,比如调用最新文章、热门文章、自定义文章类型文章循环输出等,和query_posts()函数具有相同的查询功能,但优于query_posts()函数。

使用栗子

<?php
// 用于查询的参数或者参数集合
$args = array(
); 
// 使用参数并实例化WP_Query
$the_query = new WP_Query( $args );
 
// 判断查询的结果,检查是否有文章
if ( $the_query->have_posts() ) :
 
    // 通过查询的结果,开始主循环
    while ( $the_query->have_posts() ) :
        $the_query->the_post(); //获取到特定的文章
 
        //输出的内容
 
    endwhile;
endif;
 
// 重置请求数据
wp_reset_postdata();
?>

对应的参数

<?php 
$args = array(
    //作者参数
    'author' => '1,2,3,',
    'author_name' => 'endfish',
    'author__in' => array( 2, 6 ),
    'author__not_in' => array( 2, 6 ),
    //类别参数
    'cat' => 5,
    'category_name' => 'staff,news', //或staff+news
    'category__and' => array( 2, 6 ),
    'category__in' => array( 2, 6 ),
    'category__not_in' => array( 2, 6 ),
    //标签参数
    'tag' => 'cooking',
    'tag_id' => 5,
    'tag__and' => array( 2, 6),
    'tag__in' => array( 2, 6),
    'tag__not_in' => array( 2, 6),
    'tag_slug__and' => array( 'red', 'blue'),
    'tag_slug__in' => array( 'red', 'blue'),
    //分类参数(自定义分类法)
    'tax_query' => array(
        'relation' => 'AND', //'AND' or 'OR'
        array(
            'taxonomy' => 'color',
            'field' => 'slug', //'id' or 'slug'
            'terms' => array( 'red', 'blue' ),
            'include_children' => true,
            'operator' => 'IN' //'IN', 'NOT IN', 'AND'
        ),
        array(
            'taxonomy' => 'actor',
            'field' => 'id',
            'terms' => array( 103, 115, 206 ),
            'include_children' => false,
            'operator' => 'NOT IN'
        )
    ),
    //文章和页面参数
    'p' => 1,
    'name' => 'hello-world',
    'page_id' => 1,
    'pagename' => 'sample-page',
    'pagename' => 'contact_us/endfish',
    'post_parent' => 1,
    'post_parent__in' => array(1,2,3),
    'post_parent__not_in' => array(1,2,3),
    'post__in' => array(1,2,3),
    'post__not_in' => array(1,2,3),
    'has_password' => true,
    'post_password' => 'multi-pass',
    'post_type' => array(
        'post',
        'page',
        'revision',
        'attachment',
        'my-post-type',
    ),
    'post_type' => 'any',
    'post_status' => array(        
        'publish',
        'pending',
        'draft',
        'auto-draft',
        'future',
        'private',
        'inherit',
        'trash'
    ),
    'post_status' => 'any',
    //分页参数
    'posts_per_page' => 10,
    'posts_per_archive_page' => 10,
    'nopaging' => false,
    'paged' => get_query_var('paged'),
    'nopaging' => false,
    'posts_per_archive_page' => 10,
    'offset' => 3,
    'paged' => get_query_var('paged'),
    'page' => get_query_var('page'),
    'ignore_sticky_posts' => false,
    //排序参数
    'order' => 'DESC', //'ASC' or 'DESC'
    //'none' or 'ID' or 'author' or 'title' or 'name' or 'date'
    //'modified' or 'parent' or 'rand' or 'comment_count' or 'menu_order'
    //'meta_value' or 'meta_value_num' or 'title menu_order' or 'post__in'
    'orderby' => 'date',
    //日期参数
    'year' => 2019,
    'monthnum' => 4,
    'w' =>  25,
    'day' => 17,
    'hour' => 13,
    'minute' => 19,
    'second' => 30,
    'm' => 201404,
    'date_query' => array(
      array(
        'year' => 2014,
        'month' => 4,
        'week' => 31,
        'day' => 5,
        'hour' => 2,
        'minute' => 3,
        'second' => 36,
        'after'  => 'January 1st, 2013',
        'before' => array(
          'year'  => 2013,
          'month' => 2,
          'day'   => 28,
        ),
        'inclusive' => true,
        'compare' =>  '=',
        'column' => 'post_date',
        'relation' => 'AND',
      ),
    ),
    //自定义字段参数
    'meta_key' => 'key',
    'meta_value' => 'value',
    'meta_value_num' => 10,
    //'=', '!=', '>', '>=', '<', '<='
    'meta_compare' => '=',
    'meta_query' => array(
       'relation' => 'AND', //'AND', 'OR'
       array(
         'key' => 'color',
         'value' => 'blue',
         'type' => 'CHAR',
         'compare' => '='
       ),
       array(
         'key' => 'price',
         'value' => array( 1,200 ),
         'compare' => 'NOT LIKE'
       )
    ),
    //权限参数
    'perm' => 'readable',
    //缓存参数
    'cache_results' => true,
    'update_post_term_cache' => true,
    'update_post_meta_cache' => true,
    'no_found_rows' => false,
    //搜索参数
    's' => $s,
    'exact' => true,
    'sentence' => true,
    'fields' => 'ids'
);
?>