Back to Community

Fixing WP-PageNavi Pagination Issues in Shortcodes and Custom Loops

8 threads Sep 9, 2025 PluginWp-pagenavi

Content

If you're using the WP-PageNavi plugin and finding that pagination isn't working within your custom shortcodes, loops, or templates, you're not alone. This is one of the most common issues users encounter when implementing custom queries in WordPress. Let's break down why this happens and explore the most effective solutions.

Why Pagination Breaks in Custom Contexts

The core issue typically stems from how WordPress handles main queries versus custom queries. WP-PageNavi is designed to work with the main WordPress query. When you create a custom WP_Query inside a shortcode or template, the plugin doesn't automatically recognize it, leading to several symptoms:

  • Clicking page links keeps you on the same page
  • URL doesn't change to include /page/2/
  • Pagination links don't appear at all
  • Pagination shows "Page 1 of 1" despite multiple pages of content

Common Solutions for WP-PageNavi Issues

1. Properly Setting the Paged Parameter

The most critical step is ensuring your custom query correctly identifies the current page. Use this robust method to set the paged parameter:

$paged = (get_query_var('paged')) ? get_query_var('paged') : (get_query_var('page') ? get_query_var('page') : 1);

$args = array(
    'post_type' => 'your_custom_post_type',
    'posts_per_page' => 10,
    'paged' => $paged
);

$custom_query = new WP_Query($args);

2. Using the 'echo' => false Parameter for Shortcodes

When using WP-PageNavi within a shortcode (which must return content, not echo it directly), use the echo parameter:

// Instead of: wp_pagenavi(array('query' => $custom_query));

// Use this to return the HTML for output in your shortcode:
$pagination = wp_pagenavi(array('query' => $custom_query, 'echo' => false));

// Then return it as part of your shortcode output:
return $output . $pagination;

3. Resetting Post Data Correctly

After your custom loop, always reset post data to avoid conflicts with other queries on the page:

// After your custom loop
wp_reset_postdata();

4. Ensuring Proper Query Variable Handling

For pagination to work on static front pages, you may need to add this to your functions.php file:

add_filter('redirect_canonical', 'disable_redirect_canonical_for_pagination');
function disable_redirect_canonical_for_pagination($redirect_url) {
    if (is_page() && get_query_var('paged')) {
        return false;
    }
    return $redirect_url;
}

Complete Shortcode Example

Here's a complete example of a shortcode that properly implements WP-PageNavi:

function custom_post_shortcode($atts) {
    $output = '';
    
    // Get current page
    $paged = (get_query_var('paged')) ? get_query_var('paged') : (get_query_var('page') ? get_query_var('page') : 1);
    
    // Query arguments
    $args = array(
        'post_type' => 'your_post_type',
        'posts_per_page' => 5,
        'paged' => $paged
    );
    
    $custom_query = new WP_Query($args);
    
    // The Loop
    if ($custom_query->have_posts()) {
        while ($custom_query->have_posts()) {
            $custom_query->the_post();
            $output .= '
' . get_the_title() . '
'; } // Get pagination HTML if (function_exists('wp_pagenavi')) { $pagination = wp_pagenavi(array('query' => $custom_query, 'echo' => false)); $output .= $pagination; } wp_reset_postdata(); } return $output; } add_shortcode('custom_posts', 'custom_post_shortcode');

When These Solutions Might Not Work

If you've implemented all these solutions and pagination still doesn't work, the issue might be:

  • A theme or plugin conflict - try disabling other plugins temporarily
  • Permalink structure issues - try resaving your permalinks in Settings > Permalinks
  • Custom rewrite rules interfering with pagination

Remember that WP-PageNavi is a powerful tool, but it requires proper implementation within WordPress's query system. By ensuring your custom queries properly handle the paged parameter and using the echo parameter correctly in shortcodes, you can resolve most pagination issues.