The easiest way to change posts per page in a template (WordPress)

Surprisingly simple

By Josh Robbs
November 19, 2018
Filed under:
Skill level: Master
Task: Change the number of posts displayed in a specific WordPress archive
You need:
  • A self-hosted WordPress website
  • A safe place to store a function on your website. This can be in the functions.php file in a child theme or in a plugin.

Questions Answered

  1. How to change posts per page for custom post type archive
  2. How to change posts per page for custom taxonomy archive

If your site has custom post types, you may want to change the posts per page in an archive. It's easy if you want to change everything to the same count. But what if you only want to change the posts per page for 1 archive?

How would you use this?

If you're looking at an archive of blog posts or articles, you only want a few visible - maybe up to 10 depending on the layout. But what if you have a directory in addition to your articles?

A directory listing can be much shorter than an article with an excerpt. Only 6 or 10 listings per page can look bad and be a pain to navigate. How do you increase the post per page for that page alone?

The standard edit your code at your own risk disclaimer

Whenever you edit your code, you risk breaking breaking something. That is why should never work on your live website.

Steps you can take to protect yourself in case something goes wrong:

  1. Back up the site.
  2. Make copies of the files you're editing.
  3. Test your work on a development/staging site.
  4. Theme customization should be done to a child theme.
  5. Edit via external text editor. If you are editing via WordPress and WordPress goes down, you can't fix your error.
  6. Put custom code in your own plugin. Do not make edits to existing plugins.
Learn more about adding code to your website safely.

The code

add_action( 'pre_get_posts', 'directory_query',100 );
function directory_query( $query ) {
	if ( ! is_admin() && ( $query->is_tax('directory') )) {
		$query->set('posts_per_page', '8');
	} 
}

Making the code your own

You do have to do a little homework to make this work.

1. ! is_admin() && ( $query->is_tax('directory')
That tells WordPress when to run the script. This example is for a custom taxonomy archive called directory. You need to change the bit after "->" to meet your needs. WordPress has a ton of conditional tags you can use.

2. directory_query
That is the function name and is used twice. If you use this script more than once, you'll need to rename the function. The name doesn't really matter as long as it isn't used by something else.

3. $query->set('posts_per_page', '8');
Would you believe that this sets the posts per page to 8? It does. Change the number to whatever suits your needs.

Useful?

Useful? Wow, that's tragic. That might be the worst newsletter pitch headline I've ever written.

As unsexy as it may be, my goal for this site is to be simple and useful. If you'd like to know when I add a new article, get on the list.

It's an automated newsletter that will notify you when I make a new post - once a week at most. The closest you'll get to a sales pitch is when I announce webinars.

Josh started WP Master Builder to help small businesses get more done on their website in less time. He’s also a marketer and designer. There’s always a chance that he’s watching “Sky Captain and the World of Tomorrow” again or helping his wife build pipe lamps.

Submit a Comment

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Learn something?

Share it so others can use it too!