In previous post How to create Custom Post Type in WordPress? we have discussed on custom post type, how easily we can create it with our generic function call for multiple custom post types. Here we are going to see how to add simple navigation and numbered pagination for custom posts.

I am assuming that you have custom post ‘Books’ with ‘book’ slug present in database.

Displaying custom posts on a page

Now lets say you want to display 10 book list on ‘Books’ page. To do that first you need to check which is the current page and if current page is ‘books’ then loop the query result.

if ( is_page ( 'books' ) ) {
  query_posts( 'post_type=book' );
  if ( have_posts()) :
    while ( have_posts() ) : the_post();
} else {
  // run default page query

Custom Post : Simple Navigation

Above code works just fine and will display book list on ‘Books’ page. But the navigation will not work with default query. You will need to provide additional parameters to let the navigation work. After page check add this code:

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts('post_type=book&posts_per_page=10&paged=' . $paged );

and default navigation after endwhile;

global $wp_query;
if ( $wp_query->max_num_pages > 1 ) :
  echo '<nav id="nyb_post_pagi">';
  next_posts_link( 'Older posts' );
  previous_posts_link( 'Newer posts' );
  echo '</nav>';

By doing this your navigation will work with custom post type with ‘Older Posts’ / ‘Newer Posts’ or whatever labels you have for navigation.

Custom Post : Numbered Pagination

Is ‘Older Posts/Newer Posts’ looks boring? or irrelevant with what you have looped? or do you have lots of posts in this custom post type? It means you need numbered pagination now. Ok. Lets have that ready in functions.php!

function nyPaginate($end_size = 1, $mid_size = 5, $show_prev_next = true, $prev_text = '< Previous', $next_text = 'Next >', $pagi_type = 'list') {
  global $wp_query;

  $big = 999999999; // need an unlikely integer

  $args = array(
    'base'         => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
    'format'       => '?page=%#%',
    'total'        => $wp_query->max_num_pages,
    'current'      => max( 1, get_query_var('paged') ),
    'show_all'     => false,
    'end_size'     => $end_size,
    'mid_size'     => $mid_size,
    'prev_next'    => $show_prev_next,
    'prev_text'    => $prev_text,
    'next_text'    => $next_text,
    'type'         => $pagi_type, // plain, list, array
    'add_args'     => false,
    'add_fragment' => ''

  echo paginate_links( $args );

And call this function after endwhile;

nyPaginate ( $mid_size = 8, $show_prev_next = true, $prev_text = '<', $next_text = '>', $pagi_type = 'list' );

After this you will see numbered pagination for your custom post type! Now its up to you to keep both navigation or single one!


The biggest question people ask that navigation is not working on custom post type!

Possibilities navigation not to work with custom post types:

  1. Check your custom post slug – it must be unique, not already used for any post or page. If already in used then you need to update ‘post_type‘ slug.
  2. You might have ‘Books’ page with ‘books’ slug. And you also might have ‘Books’ custom post with post_type ‘books’. In this case wordpress will stuck at how to execute. Better avoid using same slug for custom post type.
  3. Getting 404 page? As mentioned above slug of page or post and custom post type causes this issue. Either change the post type slug or (better way) change the slug of similar page or post.
  4. Navigation is not displaying – Ok all your code is correct. Did you forgot to add $paged… check?
  5. Will pagination work with WP_Query() – Yes, go ahead!
  6. Navigation not working with permalink – Check your permalink settings. Try updating permalink settings to make an effect in .htaccess.

Did you gone through How to create Custom Post Type in WordPress?

You might also like: