Support » Plugins and Hacks » WP_Query skipping pages

  • Resolved Southbound

    (@southbound)


    I’m debugging a plugin, and found something strange I could isolate to this short code. It counts the number of pages in the DB:

    `$myQuery = new WP_Query();
    $myQuery->query(‘post_type=page’);
    $numberOfPages = 0;
    while ($myQuery->have_posts())
    {
    $numberOfPages++;
    $myQuery->the_post();
    }
    echo $numberOfPages;`

    The problem is I get 10, while there are 12 published pages in the DB. How can that happen?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Probably affected by what the Administration > Settings > Reading Blog pages show at most is set to?

    Yay! Thank you so much! I could never have guessed that setting would have anything to do with it. When I changed it to 20, I got my 12 pages.

    I suspect it’s the have_posts() function that limits the number of posts (pages). Is there another way to write the loop so that I get all page records at once, instead of a certain number at a time?

    posts_per_page=-1

    See:
    query_posts()

    Thanks again, but I read that query_posts() is used to modify the main loop, which is probably not what I want. My plugin is about excluding some pages to show up in the list of pages, so it’s a separate loop, right? I’m kind of stuck with the WP_Query object, according to the docs.

    You can use the query_post arguments with a new WP_Query

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘WP_Query skipping pages’ is closed to new replies.