WordPress: Limit / Change number of posts in archive pages /category

How to limi/ change number of posts in wordpress archive pages, category, tagRecently a visitor asked us how to change the number of posts per page in wordpress. We simply suggested him to change the number of posts in reading settings, but actually he didn’t mean it. He wished to change the number of posts displayed only on category pages and not on home page or any other archive pages. The value that you enter in your wordpress reading settings is applied globally to all your blog pages. Also there is no option in your wordpress dashboard to limit or change the number of posts on archive pages.

Let us assume that you have set 10 posts per page in your wordpress reading settings. Now all the pages such as home page or blog page and archive pages (category, tag, date and author pages) will display 10 posts in every single page. This is your global setting, but what if you wish to show 50 posts on category pages and 20 posts on tag pages. That is where wordpress conditional tags come in. Here in this post we will see how to change number of posts on archive pages.

Note that archive page in wordpress includes category page, tag page, author page and date based pages.

How to limit / change number of posts in archive pages

Login to your wordpress site, go to Appearance >> editor and open Theme functions (functions.php) file.

{OR} Using FTP or cPanel access your child theme/ themes functions.php file and paste the following code.

/**
 * Limit, change number of posts in archive pages
 */
add_filter('pre_get_posts', 'limit_change_posts_archive');
function limit_change_posts_archive($query){
    if ($query->is_archive) {
        $query->set('posts_per_page', 7);
    }
    return $query;
}

The above code will display 7 posts (each page) on all archive pages that is category page, tag page, author page and date based pages. By using conditional tags you can limit the values only to category page or tag page.

Learn more about conditional tags here and you can use this list of conditional tags for reference.

For instance if you change the $query->is_archive to $query->is_category then this will be applied only to category pages. Similarly you can use it for tag pages by adding is_tag. Also you can combine conditional tags just like this which applies only to category and tag pages.

if ($query->is_category || is_tag) {
        $query->set('posts_per_page', 7);
    }

You can also display posts in category pages and tag pages in different numbers just by adding two separate piece of code in your functions.php file. Other than that you can also limit posts using category ID and tag ID.

Related: How to find category ID, tag ID, post ID in wordpress

Case 1:  Display 50 posts in category pages

/**
 * Limit, change number of posts in category pages
 */
add_filter('pre_get_posts', 'limit_change_posts_category');
function limit_change_posts_category($query){
    if ($query->is_category) {
        $query->set('posts_per_page', 50);
    }
    return $query;
}

Case 2:  Display 20 posts in tag pages

/**
 * Limit, change number of posts in tag pages
 */
add_filter('pre_get_posts', 'limit_change_posts_tag');
function limit_change_posts_tag($query){
    if ($query->is_tag) {
        $query->set('posts_per_page', 20);
    }
    return $query;
}

Hope this post helped you on how to limit change number of posts in archive pages. Share it and feel free to drop your comments below.