【debug】wordpress使用ignore_sticky_posts代替caller_get_posts

【debug】wordpress使用ignore_sticky_posts代替caller_get_posts

在使用query_posts输出首页热门文章排序,会出现最新的置顶文章,需要排除,但打开wordpress调试模式后,发现WP给出了如下提示:

Notice: 自3.1.0版本起,已不建议给WP_Query传入一个参数!“”不再被建议使用。请改用“”。 in XXXX\XXXX\wp-includes\functions.php on line 3891

ignore_sticky_posts 意为是否显示置顶最新文章在query_posts控制的列表上,默认值为0,不排除置顶文章。请注意:ignore/exclude sticky posts being included at the beginning of posts returned, but the sticky post will still be returned in the natural order of that list of posts returned(在返回文章列表时,仍以自然顺序将曾经置顶的文章安插在列表中,也就是说你排除的置顶文章只适用于某文章列表上).

用法举例

只显示(返回)第一篇置顶文章:

$sticky = get_option( ‘sticky_posts’ );  
$query = new WP_Query( ‘p=’ . $sticky[0] );

返回第一篇置顶文章,否则显示最新的以发布的文章:

$args = array(  
    ‘posts_per_page’ => 1,  
    ‘post__in’  => get_option( ‘sticky_posts’ ),  
    ‘ignore_sticky_posts’ => 1  
);  
$query = new WP_Query( $args );

排除查询中所有的置顶文章:

$query = new WP_Query( array( ‘post__not_in’ => get_option( ‘sticky_posts’ ) ) );

返回某一分类下所有文章,不在文章列表上显示置顶文章。但其置顶文章仍显示在其他自然排序中,比如日期排序归档:

$query = new WP_Query( array( ‘ignore_sticky_posts’ => 1, ‘posts_per_page’ => 3, ‘cat’ => 6 );

更多关于ignore_sticky_posts的介绍与例子请参见WP官方:WP_Query函数