WordPress query_posts result differs from wp_query request result

I have a multisite WordPress setup (3.5.1, I cannot risk updating) on which I am querying some posts after having switched to the correct blog.

I have a query_posts function with the following parameters:

Read More
$posts = query_posts('posts_per_page=5&paged=1&post_status=publish&orderby=date&order=desc');

from this query of posts the query_vars becomes ($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] => 
)

The query WordPress says it executes to find these posts is then ($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

If I execute this query on the database the resulting IDs are:

12059
12046
12038
12030
12026

However, if I loop over the posts:

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

I get the following ID’s:

11741
11721
11643
12059
12046
12038
12030
12026

Does anyone know how this is possible? The array $posts is empty before calling query_posts and I do not understand why these posts occur in the array. If I select these posts in my MySQL database by hand there is no noticeable difference between them that would make the first three special in some way, and all three are older than the other 5 requested.

Thanks in advance!

Related posts

Leave a Reply

1 comment

  1. Did you try to see what post type are you getting on both queries? I remember once I had this issue where I was querying post using wp_query and query_posts and in one instance, I was getting only POSTs and in other instance I was getting all Pages / Media Attachment and Posts