Support » Developing with WordPress » Custom Post Type and Pagination

  • Resolved ecohostingservices


    I would like to get some pagination on my page that uses a Custom Post Type. I have tried various plugins and they don’t work.

    I have managed to find some code that does work but it only allows for 2 pages.

    <div class="post-list row text-center">
    <?php if ( get_query_var( 'paged' ) ) {
        $paged = get_query_var( 'paged' );
     } else if ( get_query_var( 'page' ) ) {
        // This will occur if on front page.
        $paged = get_query_var( 'page' );
     } else {
         $paged = 1;
     $my_query = new WP_Query( array(
         'post_type'           => 'grave',
         'posts_per_page'      => 10,
         'paged'               => $paged,
          'tax_query' => array(
               array (
                   'taxonomy' => 'gentry',
                   'field' => 'slug',
                   'terms' => 'gentries',
     ) ); ?>
     <?php if (have_posts()) { ?>
          <?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
    	  <?php get_template_part('template-parts/content', 'grave');
           } else { ?>
              <p>Nothing Found</p>
              <?php get_template_part('template-parts/content', 'none'); ?>
          <?php } ?>
    <div class="navigation-c">
       <?php printf( '<div>%s</div>', get_next_posts_link( 'Older posts', $my_query->max_num_pages ) );
       printf( '<div>%s</div>', get_previous_posts_link( 'Newer posts', $my_query->max_num_pages ) );?>

    How can I get pagination to work with more pages please?


Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz


    Your pagination issue is not due to your post type, it’s due to your custom query. Almost all pagination functions rely upon the page’s main query to work out the pagination. These functions know nothing of your custom query. The one exception is paginate_links() because you pass all the data it needs, it does not look at the main query data.

    IMO, the best way to manage pagination is by not doing custom queries. You can alter the main query to do whatever your custom query does by hooking into the “pre_get_posts” action and setting the necessary query vars to get the posts you want.

    Thread Starter ecohostingservices


    Thanks for your reply bcworkz, I looked into it further and still couldn’t get it to work.

    However, I did another search and found some code and used that. It now works for my custom query. I placed the code below into my themes functions file.

    function pagination_bar() {
        global $my_query;
        $total_pages = $my_query->max_num_pages;
        if ($total_pages > 1){
            $current_page = max(1, get_query_var('paged'));
            echo paginate_links(array(
                'base' => get_pagenum_link(1) . '%_%',
                'format' => '/page/%#%',
                'current' => $current_page,
                'total' => $total_pages,

    I then added this code below to where I wanted the pagination to be:

        if (have_posts()):

    It does the job but it would have been better if the built in pagination worked. Anyhow it is a solution.


    Moderator bcworkz


    You’re welcome. If it works don’t fix it? šŸ™‚

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Custom Post Type and Pagination’ is closed to new replies.