How to Change the Number of Posts in a Category in WordPress

Posted on

Certain WordPress blogs require their content displayed in a very specific way. Some publish tiny short stories of 55 words each, others might have poetry carefully calibrated to particular screen widths. By default, WordPress shows 10 posts in every archive type page. That includes category, tag, and serach pages as well. This is an easy option to tweak. Simply go to the “Reading” section under the “Settings” menu item in the WordPress dashboard and change the appropriate option as shown below.

posts per page

But what if you want different numbers of posts for different categories? Or you want to change the number of search results that are displayed separate from the regular archive section? WordPress doesn’t provide any inbuilt way to accomplish this – at least not through the GUI. Fortunately, we have some hooks that will help us achieve the results we want. Let’s look at how to change the number of posts in a specific category section.

Using the Right Hooks

In my test example, I’m going to change the number of posts displayed for two categories on my blog – Politics, and Book Reviews. I want to category pages of the former to only display 1 post per page and 2 posts for the latter. To do this, we’re going to modify the posts query object that is run before every page is rendered. I had talked about the query_posts function when discussing the WordPress loop in general. We’re going to be using a similar technique. The “pre_get_posts” action hook allows us to manipulates the query containing the posts for a particular page before it actually executes. Since the query is passed by reference and not by value, there is no need to return anything. Any changes we make to the variable are directly reflected at the source.

Open up your functions.php file or use a custom plug-in for abstracting theme independent changes consisting of custom code. Paste the following into it:

add_filter('pre_get_posts', 'posts_in_category');

function posts_in_category($query){
    if ($query->is_category) {
        if (is_category('book-reviews')) {
            $query->set('posts_per_archive_page', 2);
        // category With ID = 32 show only 5 posts
        if (is_category('politics')){
            $query->set('posts_per_archive_page', 1);


This uses  the “pre_get_posts” hook to access the query object as a parameter. You can see that we first check whether or not the current query is a category and if so, uses the “posts_per_archive_page” sub variable to change the number of posts displayed for Book Reviews to 2 and Politics to 1. I can use either the ID or the category slug, both of which can be obtained via the “Categories” submenu under the “Posts” tab in the administration dashboard.

As mentioned earlier, we have no need to return anything back since the query object is a reference. After saving this code, I open up the category pages both for Politics and Book Reviews. As you can see below, the number of posts being displayed for each has been modified to suit our specifications.

book reviews two

politics just one

Using a Plugin

If you don’t want to change code in your functions.php file, you can make use of the Posts Per Category plugin to achieve the same results. After installation, you can go to the options screen and set up different rules for each category not just for the number of posts in the archive section, but also for the feeds as well as shown below.

posts per category

This works great if you just want to change the number of posts for categories. But it doesn’t allow you finer control over stuff like the search results etc. For that, it’s best to implement the code so that you’re able to get the exact results you want. But if category level changes are all you need, this plugin is a perfect solution without having to mess about with custom code.

4 Comments on “How to Change the Number of Posts in a Category in WordPress”!

  • Bhagwad, this is a great solution for us. Thanks for pointing it out. We want to write reports about companies, and these reports will be re-written for each company every couple of weeks. But we only want to *display* the latest report on each company. This filter allows us to do that.

    I do have two questions for you, though. 1) We’ll have about 1000 categories. So is there some way to simply limit the number of posts displayed to “1” for ALL categories in one swipe, rather than running an ‘if’ for each category?

    and 2) is there a way to suppress this filter on particular pages? So for example, if someone ‘favorites’ a particular report we would still want them to be able to see it on their favorites page.


  • Thank-you for the plugin! Very helpful!

Leave a Reply

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