Number of posts per page setting is not working?

OK, here’s my prob. I have static homepage and a separate posts page on my wordpress site. I have defined the two in the admin panel. Now, I have also set the max number of posts to be displayed to 5(in settings> reading), but all the posts(10+) are still being displayed on the blog page.

I’m also displaying the latest 3 posts on the static homepage using a custom query. i.e

Read More
$wp_query = new WP_Query( array( 'posts_per_page' =>3));

<results loop code>

But this list also is displaying more than 3 posts.It seems wordpress is ignoring the posts_per_page limit I set whether I do it at the admin panel or through code. What the hell is going on? This is my 1st time using wordpress for developing a site. Is this a common problem for noobs?

EDIT:

I just stripped the static homepage bare and left a basic code block that simply fetches titles of the posts. More than 3 posts are still being returned. Here is the actual(and only) code currently in the homepage:

<div id="content">

  <?php $wp_query = new WP_Query( array( 'posts_per_page' => 3) );?>

  <?php if ( $wp_query->have_posts() ) : ?>
  <ul>
    <?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
    <li>
      <a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
    </li>
    <?php endwhile;?>
  </ul>
  <?php endif; ?>

</div>

Related posts

Leave a Reply

4 comments

  1. This could be caused by a theme or plugin overriding the WordPress settings. You could try enabling the TwentyEleven or TwentyTen theme, as well as disabling any plugins, to see if it works then.

    If so, you could post which theme you are using (if it’s prebuilt) or which plugins you had to disable to get it working – and we can see what can be done to remedy it.

  2. I wanted to add my case to this list since it took me ages to debug, what happens is that i changed the number of posts an archive page displayed by hooking into the pre_get_post hook:

    add_filter('pre_get_posts', 'number_of_posts_on_archive');
    function number_of_posts_on_archive($query)
    {
        if(is_post_type_archive(array('post_type1', 'post_type2')))
        {
            $query->set('posts_per_page', 20);
        }
        return $query;
    }
    

    And to fix the issue where it would include the post query for a completely different post type i added something like this to the if statement:

    if( (is_post_type_archive(array('post_type1', 'post_type2'))) && ($query->get('post_type') !== 'post') )
    {
        ....
    }
    

    The reason that i am posting it here is that this is the first result that came up on google search related to the search i did for it. So it might help somebody out who can’t figure out for an hour… or two why the query just does not want to listen to you.

  3.  //just before calling your code add this and customize it as you like
     function hwl_home_pagesize( $query ) {
    if ( is_admin() || ! $query->is_main_query() )
        return;
    
    if ( is_home() ) {
        // Display only 1 post for the original blog archive
        $query->set( 'posts_per_page', 1 );
        return;
    }
    
    if ( is_post_type_archive( 'movie' ) ) {
        // Display 50 posts for a custom post type called 'movie'
        $query->set( 'posts_per_page', 50 );
        return;
      }
    }
    add_action( 'pre_get_posts', 'hwl_home_pagesize', 1 );
    
  4. N.Schipper correctly said. But the only thing in the condition is not enough condition

    ! is_admin ()

    So the number of posts per page will be changed only in the necessary cycles and will not affect the admin area.

    if( (! is_admin()) && (is_post_type_archive(array('post_type1', 'post_type2'))) && ($query->get('post_type') !== 'post') )
    {
      {
        $query->set( 'posts_per_page', 6 );
        return;
    }
    }