QUERY_POSTS结果与wp_QUERY请求结果不同

时间:2013-11-28 作者:bitXL

我有一个多站点Wordpress设置(3.5.1,我不能冒险更新),在切换到正确的博客后,我正在查询一些帖子。

我有一个带有以下参数的query\\u posts函数:

$posts = query_posts(\'posts_per_page=5&paged=1&post_status=publish&orderby=date&order=desc\');
通过此帖子查询,query\\u vars变为($GLOBALS[\'wp_query\']->query_vars):

Array (
    [posts_per_page] => 5 
    [paged] => 1 
    [post_status] => publish 
    [orderby] => date 
    [order] => desc 
    [error] => [m] => 0 
    [p] => 0 
    [post_parent] =>  
    [subpost] =>  
    [subpost_id] =>  
    [attachment] =>  
    [attachment_id] => 0  
    [name] =>  
    [static] =>  
    [pagename] =>  
    [page_id] => 0  
    [second] =>  
    [minute] =>  
    [hour] =>  
    [day] => 0  
    [monthnum] => 0  
    [year] => 0  
    [w] => 0  
    [category_name] =>  
    [tag] =>  
    [cat] =>  
    [tag_id] =>  
    [author_name] =>  
    [feed] =>  
    [tb] =>  
    [comments_popup] =>  
    [meta_key] =>  
    [meta_value] =>  
    [preview] =>  
    [s] =>  
    [sentence] =>  
    [fields] =>  
    [menu_order] =>  
    [category__in] => Array ( )  
    [category__not_in] => Array ( )  
    [category__and] => Array ( )  
    [post__in] => Array ( )  
    [post__not_in] => Array ( )  
    [tag__in] => Array ( )  
    [tag__not_in] => Array ( )  
    [tag__and] => Array ( )  
    [tag_slug__in] => Array ( )  
    [tag_slug__and] => Array ( )  
    [ignore_sticky_posts] =>  
    [suppress_filters] =>  
    [cache_results] => 1  
    [update_post_term_cache] => 1  
    [update_post_meta_cache] => 1  
    [post_type] =>  
    [nopaging] =>  
    [comments_per_page] => 50  
    [no_found_rows] => 
)
Wordpress说它执行查询来查找这些帖子是($GLOBALS[\'wp_query\']->request):

SELECT SQL_CALC_FOUND_ROWS wp_5_posts.ID 
FROM wp_5_posts 
WHERE 1=1 AND wp_5_posts.post_type = \'post\' AND (wp_5_posts.post_status = \'publish\') 
ORDER BY wp_5_posts.post_date desc LIMIT 0, 5
如果在数据库上执行此查询,则生成的ID为:

12059
12046
12038
12030
12026
但是,如果我在帖子上循环:

foreach($posts as $p){
    echo($p->ID."<br>");
}
我获得以下ID:

11741
11721
11643
12059
12046
12038
12030
12026
有人知道这是怎么可能的吗?在调用query\\u posts之前,数组$posts是空的,我不理解为什么这些posts出现在数组中。如果我手动在MySQL数据库中选择这些帖子,它们之间没有明显的区别,这会使前三篇文章在某种程度上变得特别,而且所有这三篇文章都比其他5篇文章要老。

提前感谢!

3 个回复
SO网友:Milo

您使用不当query_posts. Don\'t use query_posts, ever.

如果要迭代帖子数组,请使用get_posts 并将结果分配给非保留全局变量。

$my_posts = get_posts( array(
    \'posts_per_page\' => 5,
    \'paged\' => 1,
    \'post_status\' => \'publish\'
    \'orderby\' => \'date\',
    \'order\' => \'desc\'
) );

foreach( $my_posts as $p ){
    echo($p->ID."<br>");
}

SO网友:bitXL

这似乎并没有解决问题。我尝试了更多的选项,发现当我没有选择类别时,是“忽略粘性帖子”选项使帖子出现。

SO网友:Francesco

它们是“粘帖”吗?试着忽略它们。在您的wp\\U查询中:

\'ignore_sticky_posts\' => 1

结束

相关推荐