3

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

$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>
2
  • Based on the code you've included, you've done everything right, could you show some more of the code to help find where the error is?
    – mor7ifer
    Jan 16, 2012 at 15:15
  • this doesn't necessarily have anything to do with your issue, but you should use some var other than $wp_query for your custom queries, that's the global var WordPress uses for the main loop, and you may get unpredictable results using it for other purposes.
    – Milo
    Jan 16, 2012 at 17:10

4 Answers 4

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.

1
  • Thanks. Disabled the stupid theme and it worked! I got it from devpress.com(news theme). It is hyped as an 'advanced' parent theme and users are warned not to 'touch' anything. I guess by 'advanced' they meant badly coded, as all i did was to add the post fetching code after it's fancy slideshow.
    – JCoder
    Jan 16, 2012 at 18:33
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.

0
 //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 );
1
  • 1
    Please add an explanation to your answer: why could that solve the problem?
    – fuxia
    Feb 3, 2014 at 22:50
0

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;
}
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.