Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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:

$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!

share|improve this question
add comment

2 Answers

You are improperly using query_posts. Don't use query_posts, ever.

If you want to iterate over an array of posts, use get_posts and assign the results to a var that isn't a reserved global.

$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>");
}
share|improve this answer
add comment

That didn't seem to fix the problem. I tried some more options and figured out that it was the ignore sticky posts option that made the posts appear when I did not select a category.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.