Why Pagination is not working in my WordPress Custom Loop?

If you are searching about WordPress Pagination then click here to read our previous post on WordPress Pagination. Today, we will just work on WordPress Custom Loop Pagination. This article can be great resource if you saying pagination is not working.

Many of you must have faced the issue in pagination while working with custom loops and wondering why it is not working? There is simple reason for it, WordPress pagination works only with global $wp_query; variable. If you look at that WordPress normal loop like have_posts calls $wp_query->have_posts and the_post calls $wp_query->the_post. Note: if want to display posts from the current page and set the “paged” parameter to 1 when the query variable is not set (first page).

$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
$query = new WP_Query( array( 'paged' => $paged ) );

Syntax

// Global Variables
global $wp_query, $paged;

// Paged Parameter
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

// The Query
$query = new WP_Query( $args );

// The Loop
if ( $wp_query->have_posts() ) {
	while ( $wp_query->have_posts() ) {
		$wp_query>the_post();
		
		// Your Post Data
	
	}

	// Your Navigation Code

} else {

	// no posts found

}

// Restore original Post Data
wp_reset_postdata();

Example: Blog Title Template

<?php
// Exit if accessed directly
if ( !defined('ABSPATH')) exit;

/**
 * Template Name: Blog Title Template
 * Description: A Page Template that disables a blog with just post title
 *
 */
 
// Global Variables
global $more, $wp_query, $paged;

// Read More
$more = 0;

// Paged Parameter
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

// The Query
$blog_query = new WP_Query( array( 'post_type' => 'post', 'paged' => $paged ) );

// The Loop
if ( $blog_query->have_posts() ) { ?>
	<header class="page-header">
		<h1 class="page-title"><?php the_title(); ?></h1>
	</header>
	<?php 
	while ( $blog_query->have_posts() ) {
		$blog_query>the_post(); ?>
            <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                <header class="entry-header">
                	<h1 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'textdomain' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h1>
             	</header>
         	</article>        
	<?php		
	}
	
	//Navigation Code
	if ( $blog_query->max_num_pages > 1 ) { ?>
        <nav class="post-navigation">
            <h3 class="assistive-text"><?php _e( 'Post navigation', 'textdomain' ); ?></h3>
            <?php if ( function_exists('wp_pagenavi' ) )  { 
                wp_pagenavi();
            }
            elseif ( function_exists('wp_page_numbers' ) ) { 
                wp_page_numbers();
            }
            else { ?>	
                <div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', 'textdomain' ) ); ?></div>
                <div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'textdomain' ) ); ?></div>
            <?php 
            } ?>
        </nav><!-- #nav -->
        
    	<?php 
	}
	
} else { ?>

	<article id="post-0" class="post no-results not-found">
		<header class="entry-header">
			<h1 class="entry-title"><?php _e( 'Nothing Found', 'textdomain' ); ?></h1>
		</header><!-- .entry-header -->
	</article><!-- #post-0 -->
    
<?php 
}

// Restore original Post Data
wp_reset_postdata();

get_footer(); ?>

7 thoughts on “Why Pagination is not working in my WordPress Custom Loop?

  1. Andreas Anastasiades says:

    Hi, eventhough it’s an old post I hope I’m going to get an answer, because this is exactly what I need.
    I created my custom post type for clients, with own page template and use this code to fetch the posts;

    ‘Clients’, ‘posts_per_page’ => 30, ‘paged’ => $paged, ‘order’ => ‘asc’, ‘orderby’ => ‘title’ ) ); ? >

    have_posts() ) : $loop->the_post(); ? >

    <a href="”>

    Unfortunately I can’t get a pager, what am I missing?
    I tried stating this at the top of the page
    $paged = ( get_query_var(‘page’) ) ? get_query_var(‘page’) : 1;

    but no result :/

    Thanks!

  2. Anas Khan says:

    Wow… after a 4 hours of continuous search I found this post and it works perfect on my website.

    Thanks for the great tutorials.

Comments are closed.