How to query custom post with pagination

Shortlink:

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

You can see below code

<?php 
  $temp = $wp_query; 
  $wp_query = null; 
  $wp_query = new WP_Query(); 
  $wp_query->query('showposts=6&post_type=posttype&orderby=menu_order&order=ASC'.'&paged='.$paged); 
 
  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'); } ?>
 
<?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 https://www.wpblog.com/use-wp_query-to-create-pagination/ but still having issues in the code . Can you please let me know where i am doing mistake.

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

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

    <a href="” >

    <a href="”>

    max_num_pages);
    } ?>




    <?php get_footer();

    1. Md Abul Bashar says:

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

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!




%d bloggers like this: