Devin Walker is a San Diego based entrepreneur, developer and designer.

How to limit wp_pagenavi pagination numbers in WordPress

Posted on Mar 15, 2012 in Web Development, WordPress | 2 Comments
How to limit wp_pagenavi pagination numbers in WordPress

I ran into a pickle the other day where I had to display the latest posts in WordPress and limit the number of pages created by the wp_pagenavi plugin. On a site with thousands of posts, the plugin was returning 400+ pages. Obviously, we only wanted to show up to around 4-pages of data.

How to Limit the Number of Pages Output by wp_pagenavi

The plugin uses the max_num_pages variable to figure the number of pages it should paginate. You have to manually set this variable if you want to limit pagination. Here’s a bit of code that will do that:

Place this after your query:

Example of WP_Query() and Limit Pagination

Once you set the max_num_pages to whatever you wish, you will now see the number of pages returned by the plugin to your new number.

Remember to Check the WP-PageNavi Options

WP_PageNavi has an option panel in the backend to limit the number of pages shown. This may work for you, however, keep in mind that for my situation it wouldn’t because the “Last” link would take the user to page 400+ since it’s using the max_num_pages.

Hope this helps!


  1. Kazeem Samuel O
    February 28, 2017

    Great one. Sure helped. Thanks.

  2. arman amiri12
    May 13, 2018

    خرید گوشی
    خرید گوشی ارزان قیمت از فروشگاه اینترنتی تبلت فون
    echo paginate_links(array(
    ‘base’ => str_replace($big, ‘%#%’, get_pagenum_link($big)),
    ‘format’ => ‘?paged=%#%’,
    ‘current’ => max(1, get_query_var(‘paged’)),
    ‘total’ => $wp_query->max_num_pages