How to query custom post with pagination


Hello, Today i will teach you how to query any custom post with pagination in your WordPress theme,

You can see below code

  $temp = $wp_query; 
  $wp_query = null; 
  $wp_query = new WP_Query(); 
  while ($wp_query->have_posts()) : $wp_query->the_post(); 
//  Your Contents
<?php endwhile; ?>
<?php if (function_exists('wp_pagenavi')) { wp_pagenavi(); } else { include('navigation.php'); } ?>
  $wp_query = null; 
  $wp_query = $temp;  // Reset


and Now you can use WP-PageNavi Plugin or you can create navigation.php page then past below code in your navigation.php page

<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', 'twentyeleven' ) ); ?></div>
<div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'twentyeleven' ) ); ?></div>

or you can remove this code above query code

<?php if (function_exists('wp_pagenavi')) { wp_pagenavi(); } else { include('navigation.php'); } ?>

and input this code

       <?php the_posts_pagination(array(
            'screen_reader_text' => ' ',
             'end_size'     => 2,    
             'mid_size'     => 4, 

If you use this code then you can see numbering pagination.


2 thoughts on “How to query custom post with pagination”

  1. Alexander says:

    Hey Abul Bashar, Thanks for sharing this great tutorial as i am doing the pagination work from the tutorial but still having issues in the code . Can you please let me know where i am doing mistake.

    ‘paged’ => $paged
    $custom_query = new WP_Query( $args );

    have_posts()) :

    <a href="” >

    <a href="”>

    } ?>

    <?php get_footer();

    1. Md Abul Bashar says:

      Thanks for comment, please let me know your full code in

Leave a Reply

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

Time limit is exhausted. Please reload CAPTCHA.

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!