Title: Pagination problems with Custom Post Type.
Last modified: August 21, 2016

---

# Pagination problems with Custom Post Type.

 *  [Szyam](https://wordpress.org/support/users/szyam/)
 * (@szyam)
 * [12 years, 9 months ago](https://wordpress.org/support/topic/pagination-problems-with-custom-post-type/)
 * I’ve been reading and searching all night. I’ve had issues with CPT pagination
   before but this one is really making me crazy. I uploaded the site I’m building
   to [http://koovay.com/Kingship/](http://koovay.com/Kingship/) .
    My original 
   code was:
 *     ```
       <?php
           $temp = $wp_query;
           $wp_query = null;
           $wp_query = new WP_Query();
           $wp_query->query('showposts=20&post_type=portfolio'.'&paged='.$paged); ?>
   
           <?php while ($wp_query->have_posts()) : $wp_query->the_post();
           $featuredImage = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
           $permalink = get_permalink( $id );
           ?>
   
       -- Loop stuff --
   
           <?php endwhile; ?>
   
           <nav class="paged text-center">
           <?php previous_posts_link('« Newer') ?>
           <?php next_posts_link('Older »') ?>
           </nav>
   
           <?php
           $wp_query = null;
           $wp_query = $temp;  // Reset
           ?>
       ```
   
 * Im using Custom Post Types UI and created my CPT as such:
 *     ```
       add_action('init', 'cptui_register_my_cpt_portfolio');
           function cptui_register_my_cpt_portfolio() {
           register_post_type('portfolio', array(
           'label' => 'Portfolio',
           'description' => 'Your portfolio for showcasing yo bitch ass work!',
           'public' => true,
           'show_ui' => true,
           'show_in_menu' => true,
           'capability_type' => 'post',
           'map_meta_cap' => true,
           'hierarchical' => false,
           'rewrite' => array('slug' => 'works', 'with_front' => 1),
           'query_var' => true,
           'has_archive' => true,
           'menu_position' => '5',
           'supports' => array('title','editor','thumbnail'),
           'taxonomies' => array('category'),
           'labels' => array (
           'name' => 'Portfolio',
           'singular_name' => 'Work',
           'menu_name' => 'Portfolio',
           'add_new' => 'Add Work',
           'add_new_item' => 'Add New Work',
           'edit' => 'Edit',
           'edit_item' => 'Edit Work',
           'new_item' => 'New Work',
           'view' => 'View Work',
           'view_item' => 'View Work',
           'search_items' => 'Search Portfolio',
           'not_found' => 'No Portfolio Found',
           'not_found_in_trash' => 'No Portfolio Found in Trash',
           'parent' => 'Parent Work',
           )
           ) ); }
       ```
   
 * I read a post regarding changing rewrite name to something other than the CPT
   name so I changed it to “work”.
 * But this triggered a 404. I’ve tried a few alt. versions of running the loop 
   and pagination I can post.
 * Honestly, I am really hoping someone can shoot me some help. Going crazy…
    Thanks!

Viewing 6 replies - 1 through 6 (of 6 total)

 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [12 years, 9 months ago](https://wordpress.org/support/topic/pagination-problems-with-custom-post-type/#post-4320695)
 * Try it with a query like this:
 *     ```
       <?php
       // set the "paged" parameter (use 'page' if the query is on a static front page)
       $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
   
       $the_query = new WP_Query( 'posts_per_page=20&post_type=portfolio&paged=' . $paged );
       ?>
       <?php
       while ( $the_query->have_posts() ) : $the_query->the_post();
   
       $featuredImage = wp_get_attachment_url( get_post_thumbnail_id( $post->ID ) );
       $permalink = get_permalink( $id );
       // -- Loop stuff --
       ?>
   
       <?php endwhile; ?>
   
       <nav class="paged text-center">
       <?php previous_posts_link( '« Newer' ); ?>
       <?php next_posts_link( 'Older »', $the_query->max_num_pages ); ?>
       </nav>
       <?php wp_reset_postdata(); ?>
       ```
   
 * [http://codex.wordpress.org/Function_Reference/next_posts_link#Usage_when_querying_the_loop_with_WP_Query](http://codex.wordpress.org/Function_Reference/next_posts_link#Usage_when_querying_the_loop_with_WP_Query)
 *  Thread Starter [Szyam](https://wordpress.org/support/users/szyam/)
 * (@szyam)
 * [12 years, 9 months ago](https://wordpress.org/support/topic/pagination-problems-with-custom-post-type/#post-4320720)
 * Thanks, Ill try this. When it says to use ‘page’ for for static front page their
   referring to the two ‘paged’ in single quotes and not the variable ‘$paged’ correct?
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [12 years, 9 months ago](https://wordpress.org/support/topic/pagination-problems-with-custom-post-type/#post-4320726)
 * That’s correct. If it’s a static front page use:
 *     ```
       $paged = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1;
       ```
   
 *  Thread Starter [Szyam](https://wordpress.org/support/users/szyam/)
 * (@szyam)
 * [12 years, 9 months ago](https://wordpress.org/support/topic/pagination-problems-with-custom-post-type/#post-4320798)
 * Hey keesiemeijer, this code has got me further. The ‘Page’ vs ‘Paged’ thing was
   something I was not aware of. I was just using WP’s ‘front-page.php’ for showing
   a custom front page, but see that this ‘page’ is for when using the ‘static’ 
   front page option in Settings/Reading.
 * So, after creating a page that calls a template, where I now have this code being
   called, I get the static front page showing 20 posts, when I click on ‘Older’,
   it displays the last 20 posts but no way to access posts in the middle? And when
   on page/2 which is showing the last 20 posts, there is only the ‘Older’ link 
   which refreshes that page. The ‘Newer’ link is not showing up? Any ideas? Thanks
   so much!
 *  Thread Starter [Szyam](https://wordpress.org/support/users/szyam/)
 * (@szyam)
 * [12 years, 9 months ago](https://wordpress.org/support/topic/pagination-problems-with-custom-post-type/#post-4320800)
 * Apparently some of my previous code worked when I replaced the ‘paged’ with ‘
   page’. I’ve been working with WordPress for over a year and haven’t encountered
   this caveat while working with CPT’s. I’d prefer to use WP’s front-page.php vs
   a template, however when I use that, with ‘paged’ for the variable it still throws
   a 404. A battle for another day.
    I’m going to paste my working code here, for
   others who may find this problem. Note: I am using some additional code in my
   functions.php file from 320press foundation framework which I have used on a 
   few projects. It contains the necessary code for the page_navi. Again keesiemeijer,
   thanks so much for pointing this out!!
 *     ```
       <?php $paged = (get_query_var('page')) ? get_query_var('page') : 1;
           //$parent = $post->ID; /* Show child pages - Delete if not needed */
   
           $args = array('posts_per_page' => 20,'post_type' => 'portfolio', 'paged' => $paged );
           $wp_query = new WP_Query($args);
   
           if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post();
           $featuredImage = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
           $permalink = get_permalink( $id );
       ?>
       ```
   
 * <!– Loop Stuff –>
 *     ```
       <?php endwhile; endif; ?>
   
         <?php if (function_exists('page_navi')) { ?>
   
           <?php page_navi(); ?>
   
             <?php } else { ?>
   
               <nav class="wp-prev-next">
                 <div class="page-nav">
                   <div class="nav-previous"><?php next_posts_link('<< Next'); ?></div>
                   <div class="nav-next"><?php previous_posts_link('Back >>'); ?></div>
                 </div>
               </nav>
   
             <?php } ?>  
   
       <?php wp_reset_query(); ?>
       ```
   
 *  Thread Starter [Szyam](https://wordpress.org/support/users/szyam/)
 * (@szyam)
 * [12 years, 9 months ago](https://wordpress.org/support/topic/pagination-problems-with-custom-post-type/#post-4320807)
 * Well I spoke slightly too soon. The numbered pagination was working accordingly,
   ie: clicking on 3 took me to page 3 and so on. However, the next and previous
   links did not work. Only pointed at page 2. I’ve managed to find a post that 
   provided a solution but I feel its a little more heavy handed then should be.
   I’ll provide it below but if you (or someone else) may have another idea/answer
   I’d love to hear it. Thanks!
 *     ```
       <?php
           //Fix homepage pagination
           if ( get_query_var('paged') ) { $paged = get_query_var('paged'); } else if ( get_query_var('page') ) {$paged = get_query_var('page'); } else {$paged = 1; }
   
           $temp = $wp_query;  // re-sets query
           $wp_query = null;   // re-sets query
           $args = array( 'post_type' => 'portfolio', 'orderby'=>'date', 'order'=>'DESC', 'posts_per_page' => 20, 'paged' => $paged);
           $wp_query = new WP_Query();
           $wp_query->query( $args );
           while ($wp_query->have_posts()) : $wp_query->the_post();
           $featuredImage = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
           $permalink = get_permalink( $id );
       ?>
       ```
   
 * <!– Loop Stuff –>
 *     ```
       <?php endwhile; ?>
       <nav>
          <?php paginate();
          $wp_query = null;
          $wp_query = $temp; // Reset
          ?>
       </nav>
       ```
   
 * Then this needs to be placed into the functions.php
 *     ```
       function paginate() {
       global $wp_query, $wp_rewrite;
       $wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;
   
       $pagination = array(
           'base' => @add_query_arg('page','%#%'),
           'format' => '',
           'total' => $wp_query->max_num_pages,
           'current' => $current,
           'show_all' => true,
           'type' => 'list',
           'next_text' => 'Next »',
           'prev_text' => '« Previous'
           );
   
       if( $wp_rewrite->using_permalinks() )
           $pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 'page', get_pagenum_link( 1 ) ) ) . '?page=%#%/', 'paged' );
   
       if( !empty($wp_query->query_vars['s']) )
           $pagination['add_args'] = array( 's' => get_query_var( 's' ) );
   
       echo paginate_links( $pagination );
       }
       ```
   

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Pagination problems with Custom Post Type.’ is closed to new replies.

## Tags

 * [pagination](https://wordpress.org/support/topic-tag/pagination/)

 * 6 replies
 * 2 participants
 * Last reply from: [Szyam](https://wordpress.org/support/users/szyam/)
 * Last activity: [12 years, 9 months ago](https://wordpress.org/support/topic/pagination-problems-with-custom-post-type/#post-4320807)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
