WordPress.org

Forums

[resolved] WP_Query skipping pages (6 posts)

  1. Southbound
    Member
    Posted 5 years ago #

    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?

  2. MichaelH
    Member
    Posted 5 years ago #

    Probably affected by what the Administration > Settings > Reading Blog pages show at most is set to?

  3. Southbound
    Member
    Posted 5 years ago #

    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?

  4. MichaelH
    Member
    Posted 5 years ago #

    posts_per_page=-1

    See:
    query_posts()

  5. Southbound
    Member
    Posted 5 years ago #

    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.

  6. MichaelH
    Member
    Posted 5 years ago #

    You can use the query_post arguments with a new WP_Query

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.