How to limit posts per page in Wordpress

The code example is for showing 2 posts per page when any Category an search archive page is being displayed.

Choose and add the following lines to your Classipress functions.php file:

 function limit_posts_per_archive_page() {
       if ( is_category() )
          $limit = 2;
       elseif ( is_search() )
          $limit = 2;
          $limit = get_option('posts_per_page');

       set_query_var('posts_per_archive_page', $limit);
    add_filter('pre_get_posts', 'limit_posts_per_archive_page');


You don’t need to write your own code to limit posts per page in WordPress.

From the admin control panel go to Settings then Readings and change the number in the top input field to the number of posts you want per page.

You are right, this code is useful if you want a different number of post in categories and search results. For example you can have 5 post in the index, 10 posts into categories and 20 posts is the search results. This can not be done from the Wordpress control panel.