WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In the wordpress settings => Reading => Blog pages show at most [input field] posts

I have it set to 3 posts at the moment.

On my index, date archives, tag archives, category archives, search results, etc... All pages that use the loop and paging, it shows 3 posts per page now.

My goal is to be able to have different number of results for different pages. ON my index maybe have 3 posts but on search results or archives, show a different number of results per page.

Any ideas how to do this?

share|improve this question
up vote 11 down vote accepted

This will do it: (add to your theme's functions.php)

add_action( 'pre_get_posts',  'set_posts_per_page'  );
function set_posts_per_page( $query ) {

  global $wp_the_query;

  if ( ( ! is_admin() ) && ( $query === $wp_the_query ) && ( $query->is_search() ) ) {
    $query->set( 'posts_per_page', 3 );
  }
  elseif ( ( ! is_admin() ) && ( $query === $wp_the_query ) && ( $query->is_archive() ) ) {
    $query->set( 'posts_per_page', 5 );
  }
  // Etc..

  return $query;
}
share|improve this answer
    
Is set a method of $query? – Rutwick Gangurde Oct 11 '11 at 4:34
    
@RutwickGangurde Yes. In fact, is_search() and is_archive() are too, which is now a little more clear after updating my answer with info gleaned from this great post: billerickson.net/customize-the-wordpress-query That article also notes that we'll have the $wp_query->is_main_query() method in WP3.3, which is pretty cool. – goto10 Oct 11 '11 at 5:10
    
Awesome man! Thanks a lot for the great post. I never knew these methods, and I used to drill down the object / array to change / set values. Good for on the fly hacks! +1 for you! – Rutwick Gangurde Oct 11 '11 at 5:20
    
Thanks, I appreciate it! – goto10 Oct 11 '11 at 23:20
    
Since when was it necessary to never change it if is_admin dashboard? Looks like it doesn't sync the "Number of items per page:" in screen options in WP possibly resulting in pages you can't page to. – NoBugs Jan 18 at 23:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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