WordPress Pagination Code for Posts

July 11, 2018 | Code snippets , Tutorials , WordPress | by Mihovil Mikulec
wordpress post pagination code

Last update: March 1st, 2021
Tested up to: WordPress 5.6.2


  • This code will query and display WordPress posts with numeric pagination
  • Works with any WordPress theme or child-theme
  • Filters through posts, custom post types, categories, tags, authors etc. (general arguments array)
  • Works on posts page (blog), standard pages and template based pages (such as archives or search results)
  • Filters through WordPress pagination arguments array


  • Copy and paste the code below into the WP template file you want to edit
  • Tweak the code according to your needs

    $currentPage = get_query_var('paged');

    // General arguments

    $posts = new WP_Query(array(
        'post_type' => 'post', // Default or custom post type
        'posts_per_page' => 10, // Max number of posts per page
        'category_name' => 'My category', // Your category (optional)
        'paged' => $currentPage

    // Top pagination (pagination arguments)

    echo "<div class='page-nav-container'>" . paginate_links(array(
        'total' => $posts->max_num_pages,
        'prev_text' => __('<'),
        'next_text' => __('>')
    )) . "</div>";

    // Content display

    if ($posts->have_posts()) :
        while ($posts->have_posts()) :
            echo "<div class='post-wrap'>";
            echo "</div>";

    // Bottom pagination (pagination arguments)

    echo "<div class='page-nav-container'>" . paginate_links(array(
        'total' => $posts->max_num_pages,
        'prev_text' => __('<'),
        'next_text' => __('>')
    )) . "</div>";


Category pagination

In case you would like to use this code on your category or tag template to get category pagination, simply add the following line above the “general arguments” section:

$taxonomy = get_queried_object();

And inside “general arguments” section change

'category_name' => 'My category',


'category_name' => $taxonomy->slug,

(or in case of a tag template)

'tag' => $taxonomy->slug,

Search pagination

In case you would like to use this code on your search template to get search pagination, simply add the following line above the “general arguments” section:

$taxonomy = get_search_query();

And inside “general arguments” section change

'category_name' => 'My category',


's' => $taxonomy,

Need someone to take care of your WordPress website?

The easiest way to describe WordPress care services is to say that they bring peace of mind to WordPress site owners and save their time. But not only that, proper WordPress maintenance and monitoring can prevent a lot of potential problems on your website and make it perform faster and better, helping you attract and convert new clients.

  • Speed
  • Security
  • WP updates
  • Daily backups
  • Hosting
  • Uptime monitoring
  • Spam protection
  • SSL certificates
  • Malware removal
  • Form testing
  • SEO blacklist checkup
  • Database optimization
  • Activity monitoring
  • Technical support
  • Free migrations
Mihovil Mikulec
Mihovil Mikulec
Thank you for reading! I am owner of Soulstudio, WordPress developer with 10 years of experience behind me. My primary focus is on WordPress hosting, security and performance. You can contact me directly at [email protected]

6 responses to “WordPress Pagination Code for Posts”

  1. Mihovil Mikulec Rolland Wize says:

    Thanks, have been worried before

  2. Mihovil Mikulec DIANA says:

    Thanks for the help, but why does the number of page is limited to 2 ?
    When i try to click on a number superior like 3 it crash. :/

    • Mihovil Mikulec Mihovil Mikulec says:

      The issue you are having is probably related to a theme or plugin conflict. You can try to disable all the plugins and see if the problem is still there. Also, enable error_log and debug_log on your WP installation. That will give you more insight on what is going on.

  3. Mihovil Mikulec DAqwerfsdfs says:

    Thanks a lot!!!

Leave a Reply

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


Who we are

What started in 2010 as a one-man web development operation is now a small and devoted team of web developers and server administrators.

Our focus is on WordPress hosting and WordPress care services such as speed optimizations, bug fixing and security hardening.

We are located in Croatia (Europe).

Our mission

Our mission is to provide fast, stable and secure hosting environment for your WordPress websites, as well as professional WordPress care services and ongoing user support.

We believe that everybody deserves safe, stable and affordable hosting services. That is why our dedication, commitment and constant learning are reflecting that philosophy.