WordPress.org

Forums

wp_query: posts_per_page (7 posts)

  1. somtam
    Member
    Posted 1 year ago #

    Hello everybody,

    is few hours that I try to figured out why in my wp_query posts_per_page is ignored.
    I mean, I print_r and check the query. It set the LIMIT correctly, but anyway it returns more posts than the one requested.
    Moreover the property found_posts return a number that doesn't match with the LIMIT of the query.

    What can be?
    thanks so much!

  2. somtam
    Member
    Posted 1 year ago #

    I notice that it works only if I insert in the args a specific
    "category_name" => "the name"
    or
    cat = N of categories

    this doesn't make sense to me...
    PS: only with from the last 3.8 version.
    Does something change from that?

  3. alchymyth
    Forum Moderator
    Posted 1 year ago #

    how many 'sticky' posts do you have?

    what is the full code of the query?

  4. somtam
    Member
    Posted 1 year ago #

    Hi and thanks for helping!
    I make a simple test in a page, and the code is:

    $query = new WP_Query( array(
         'post_type' => 'post',
         'posts_per_page' => 2,
      )
    );
    
    if ( $query->have_posts() ) {
        while ( $query->have_posts() ) {
          $query->the_post();
          the_title();
          echo '<br>';
      }
    }
    
    wp_reset_postdata();

    if I add the 'category_name' or 'cat' everything is working fine...??!!??

  5. somtam
    Member
    Posted 1 year ago #

    I tried to add

    'ignore_sticky_posts' => true,

    and now the number of posts returned is ok.
    This stuff let me full of doubts... does it means that for a widget or a plugin that uses WP_Query and posts_per_page we have always to use ignore_sticky_posts?

  6. photocurio
    Member
    Posted 1 year ago #

    I'm having the same problem. A sticky should not add posts to the posts_per_page count. Is this a WordPress bug?

  7. somtam
    Member
    Posted 1 year ago #

    As they say in the wp_query page:

    ATTENTION If you use sticky posts, they will be included (prepended!) in the posts you retrieve whether you want it or not. To suppress this behaviour use ignore_sticky_posts

    so probably we have to ad ignore_sticky_posts always if we need the exact number of row to be retrieved.

Topic Closed

This topic has been closed to new replies.

About this Topic