How to Limit Number Of Posts on Archive Pages

Firstly, why should we limit number of posts on archive page?

We should limit number of posts on archive page when default setting are not working for us. Furthermore, it’s important to set limit to number of posts on Archive pages. This depends on our visitors and their expectations.

What is actually Archive page?

There is something called chronological nature of blogging. This means that great articles can get lost in your archives.

Therefore, creating an archives section and placing it within a  search bar and an archives widget in your sidebar can make it easier for visitors to search through your older content.

This can easily be done with inserting small code snippet to functions.php file, as follows:

* This is a function which limits Number of Posts on Archive Pages
* For our web site, desired limit is 3
function number_of_posts_on_archive($query){
if ($query->is_archive) {
$query->set('posts_per_page', 3);
return $query;
add_filter('pre_get_posts', 'number_of_posts_on_archive');

This code part for limit number of posts on archive page explains to WordPress that if $query (user action) is archive.

In that case WordPress should set attribute called posts_per_page to 3.

Also, after changes to global $query attribute, this should be returned to WordPress for further processing.

Note: Making changes to archive.php template can be very important, and here is a reason for that.

Secondly, According to WordPress template hierarchy, archive.php template is parent of these templates:

  • author.php
  • category.php
  • archive-$posttype.php
  • taxonomy.php
  • date.php
  • tag.php

This means, that (if our template has none of these child template), mechanism will resolve to archive.php, and all settings we create for archive.php will be applied to author, category, archive, taxonomy, date and tag.

Finally, In most cases, templates have at least category.php , but it’s always good to have fallback template, such as archive.php parent template.

Furthermore, when making any kind of changes to your web site, make sure to use GIT repository and to do a proper code review.

What else have you been doing with Archive pages? What problems have you encounter?