WordPress.org

Support

Support » How-To and Troubleshooting » wp_query: posts_per_page

wp_query: posts_per_page

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

Viewing 6 replies - 1 through 6 (of 6 total)
  • 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?

    how many ‘sticky’ posts do you have?

    what is the full code of the query?

    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…??!!??

    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?

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

    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.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘wp_query: posts_per_page’ is closed to new replies.
Skip to toolbar