Grow your CSS skills. Land your dream job.

Last updated on:

Paginate Custom Post Types

<?php 
  $temp = $wp_query; 
  $wp_query = null; 
  $wp_query = new WP_Query(); 
  $wp_query->query('showposts=6&post_type=news'.'&paged='.$paged); 

  while ($wp_query->have_posts()) : $wp_query->the_post(); 
?>

  <!-- LOOP: Usual Post Template Stuff Here-->

<?php endwhile; ?>

<nav>
    <?php previous_posts_link('&laquo; Newer') ?>
    <?php next_posts_link('Older &raquo;') ?>
</nav>

<?php 
  $wp_query = null; 
  $wp_query = $temp;  // Reset
?>

Comments

  1. Dutch
    Permalink to comment#

    Something went wrong. Missing code tags?

  2. ins

    Missing code here? I really want this!!

    • Jake
      Permalink to comment#

      This helped me immensely, thanks for posting. Using the 3rd post in that topic I got my taxonomy pagination working as well. Even though the author claims his doesn’t paginate properly it seems to work fine for me.

  3. Benjamin
    Permalink to comment#

    It’s ridiculous that it hasn’t been updated or at least pulled down if 90% of the code is missing.

  4. Reinaldo
    Permalink to comment#

    This don’t work!

  5. Alex Pivtorak
    Permalink to comment#

    Oh, I’ll pray to God to give you much health in your life, Chris. You just saved so much time for me.
    Thanks a lot!!!!

    to Reinaldo > it works, even without any post types. I used it on the archives.php page, which shows all my categories and archives there.

  6. Andy
    Permalink to comment#

    Hi, this seems to half way work for me. I have the custom post type displaying correctly, but when I click on the “older” link it goes to /page/2/ but the posts stay the same as the previous page. So its not showing me the older posts and the “newer” link also does not show up on /page/2/. Anny suggestions would be great! Thanks!

  7. Thanks, Worked like charm!! :)

  8. I am having the same problem as andy… I get a Older link to page 2… but upon clicking that link, page2 contains the exact same posts, and the Older link takes me to the same destination, over and over again.

    • a
      Permalink to comment#

      Did you ever get this working? I am having the same problem and would appreciate any insight. Thanks in advance.

  9. KF
    Permalink to comment#

    There are a couple of custom loop examples towards the bottom of this page:

    http://codex.wordpress.org/Pages

    For me, query_posts seems to work more reliably when it comes to pagination.

  10. Here is a link for custom post type numeric pagination on my blog -> http://www.designphilic.com/2012/08/wordpress-custom-post-type-pagination.html

  11. Beautiful!

    This worked like a charm for the Testimonials custom post type I just finished working on for a client:

    http://staging.priebeassoc.com/references/

    There’s definitely a reason I donated to your Kickstarter… If ever I Google anything CSS or WP related, CSS-tricks is always right there with the right answer.

    Thanks Chris!

  12. In this line

    
     $wp_query->query('showposts=6&post_type=news'.'&paged='.$paged); 
    

    how i do include several post types?

    fox example

    
     $wp_query->query('showposts=6&post_type=books,movies,links'.'&paged='.$paged); 
    
    • Permalink to comment#

      $wp_query->query(
      array(
      'posts_per_page
      => ‘6’,
      ‘post_type’ => array(
      ‘books’,
      ‘movies’,
      ‘links’
      ),
      ‘paged’ => $paged
      )
      )`

  13. Permalink to comment#

    Thanks for this post, it was very helpful!

  14. simon
    Permalink to comment#

    Thank you so much for posting this…. after 7 hours of pulling my hair out and trying everything, this solution enabled the next/previous pages.
    I’m using custom post type categories and combined this code as below, to select the current category and display a loop of posts in the current category

    $current_cat = get_query_var(‘cat’);
    $temp = $wp_query;
    $wp_query = null;
    $wp_query = new WP_Query();
    $wp_query->query(array(
    ‘post_type’=>’customposttypename’,
    ‘paged’ => $paged,
    ‘posts_per_page’ => 6,
    ‘cat’=> $current_cat
    ));

    while ($wp_query->have_posts()) : $wp_query->the_post();

    endwhile; // End the loop.

    <?php previous_posts_link( __( '< Newer Articles’, ‘twentyten’ ) ); ?>
    <?php next_posts_link( __( 'Older Articles >’, ‘twentyten’ ) ); ?>

    $wp_query = null;
    $wp_query = $temp; // Reset

    Hope this helps others
    This blog breaks my code so I’ve had to remove all php tags, but you can see the make up of the code

  15. marc2800
    Permalink to comment#

    Hi, i have lost 4 hours but with his code ITS WORK !

    Thanks a lot friend !

  16. Permalink to comment#

    This didn’t worked for me because i tried to did this in my homepage, that was my problem i found a simply fix for paginate custom posts types in our homepages.

    Hope this code can help someone :)

    //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;
    }
    
    $args = array('post_type' => 'custom_post_type', 'paged' => $paged );
    
    $temp = $wp_query; 
    $wp_query = null; 
    $wp_query = new WP_Query(); 
    $wp_query->query( $args ); 
    
    while( ...
    

    https://gist.github.com/3793123

    • Hi..this code is working for me right now. But there’s one problem : Page not found always showed up at the title bar. How can I fix this?

    • Permalink to comment#

      Thanks, Abel! That’s just what I needed. I was pulling my hair out, because I got the “/page/2/” structure but no new posts. Your code did the trick.

    • Harry
      Permalink to comment#

      Thanks, Abel!

  17. Permalink to comment#

    Dont work with offset

    $wp_query->query('offset=1&showposts=1&post_type=noticia&taxonomy=geral'.'&paged='.$paged);
    

    With offset all pages have first post.

    • Permalink to comment#

      Now work xD

      
      $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; 
      $offset = ( 1 * $paged ) - 0; 
      $args=array('paged'=>$paged, 'posts_per_page'=>1, 'post_type'=>'yourCPT', 'offset' => $offset); 
      query_posts($args); 
      while ( $wp_query->have_posts() ) : $wp_query->the_post(); 
      
  18. Sigh, this doesn’t work when using it on index.php :( Can’t seem to get anything working

    • Catapult
      Permalink to comment#

      Hi Amber

      Abel Cabans code a few comments up worked for me on index.php

  19. Permalink to comment#

    UPDATE!!!!

    I had two custom post types: Photo and Poetry.
    I had two pages to display the query using your code: Gallery and Poetry.

    Gallery worked. Poetry didnt. Any guesses as to why?!

    I was going CRAZY for two days now…copied and pasted random code from all around the net….

    come to find out…THE PAGE NAME CANNOT BE THE SAME AS THE POST TYPE NAME. Thats all.

    It seems that your page here is an anchor for all the others regarding CPT and Pagination. So, hopefully word gets around. I wish I could take out an ad on TV for this one

    • Permalink to comment#

      As a guide…here was my gallery.php template…for the PAGE http://www.mydomain.com/gallery

      query(‘showposts=12&post_type=photo’.’&paged=’.$paged);

      while ($wp_query->have_posts()) : $wp_query->the_post();
      ?>

                      <div class="post" class="group">
                          <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( array(120,120)); ?></a>
      
  20. Silver Gold
    Permalink to comment#

    Derek,

    Try this:

    Solution of Paging for Custom Posts Type

    Hopefully, it will help you.

  21. Permalink to comment#

    Ctrl+c
    Ctrl+v

    Works awesome. Thanks!

  22. Jake
    Permalink to comment#

    This works fine for me if I use it to display the custom post type on a page. However for some reason if I use the exact same code on ‘catalogue-archive.php’ it breaks the pagination. Any ideas on what’s going on there?

  23. Rik
    Permalink to comment#

    Hi, Thanks a lot. It works fine. I have one question. What do I have to add if I want to show custom post types from a specific Author?

    • Erwin Chrisman
      Permalink to comment#

      @Rik : You can use this

      $paged = ( get_query_var( ‘paged’ ) ) ? get_query_var( ‘paged’ ) : 1;
      $offset = ( 1 * $paged ) – 0;
      $args=array(‘paged’=>$paged, ‘posts_per_page’=>1, ‘post_type’=>’yourCPT’, ‘offset’ => $offset, ‘author_name’=>’yourname’);
      query_posts($args);
      while ( $wp_query->have_posts() ) : $wp_query->the_post();

    • Rik
      Permalink to comment#

      Hi Erwin, thanks for your reaction. it doesn’t work at all. I’m sorry.
      I found another solution. This one and it works partially.

      $temp = $wp_query;
      $wp_query = null;
      $wp_query = new WP_Query();
      $wp_query->query(‘showposts=10&post_type=listings’.’&author_name=’.$author_name.’&paged=’.$paged);

      while ($wp_query->have_posts()) : $wp_query->the_post();

      But I have another problem now. At this moment it counts for all excisting authors 8 pages with 10 posts . So when there is an author with 18 posts it also counts 8 pages instead of 2. When I click page 3 it gives a 404. What can I do about that?

  24. Mark Szymanski
    Permalink to comment#

    Is the reset used above

    $wp_query = null;
    $wp_query = $temp; // Reset

    better, or the same as

    wp_reset_postdata();

    thanks

  25. neil
    Permalink to comment#

    Here’s a weird one if I change the query variable to $loop it doesn’t work, but if its $wp_query it does????

  26. Amazing, this is the only code out there that works! (you probably already knew that)

    Im using this to click through all posts in a custom taxonomy (category).
    Is there a way to only show prev/next posts for the current taxonomy?

    • Hello Trisha,
      I am looking to do the same – paginate the current taxonomy – did you end up getting it to work?
      Thanks,
      Mark

  27. I’ve been having issues with this for awhile now and can’t see to get it working on beforetheconcert.com. The more videos button gets clicked and only goes to page 2. There should be a page 3 but not sure why it’s stopping. Not sure if its a code problem, infinite scroll problem, or custom post type problem.

    In order to get it somewhat working properly, I have the reading part in the admin set to 3 posts, I want it to display 6 posts on the homepage, category pages, etc. But when I change the code to try to that, and you click on more videos button, nothing pops up. So I have to put 3 posts in the reading section in order to getting at least working. But it’s not desired to have 3 posts per page in the subcategory templates. If I change the posts 1 posts per page it will load all the videos, but I don’t want that because it will look all weird on the category pages. Any ideas on how to get this working right??

  28. Matt Prescott
    Permalink to comment#

    Cheers Chris, worked a treat!

  29. Daniel Pollitt

    This is the fixed that worked for me. I modified Chris’ original code with just one minor change: I added is_home right after is_tag

    add_filter(‘pre_get_posts’, ‘query_post_type’);
    function query_post_type($query) {
    if(is_category() || is_tag() || is_home()) {
    $post_type = get_query_var(‘post_type’);
    if($post_type)
    $post_type = $post_type;
    else
    $post_type = array(‘post’, ‘page’, ‘videos’,’short-stories’, ‘long-stories’, ‘nav_menu_item’);
    $query->set(‘post_type’,$post_type);
    return $query;
    }
    }

  30. function wpsites_npp_navigation_links() {
    if( 'portfolio' == get_post_type() ) {?>
    <?php previous_post('&laquo; &laquo; %', 'Previous', 'no'); ?>
    | <?php next_post('% &raquo; &raquo; ', 'Next', 'no'); ?>
    <?php
    } }
    add_action('genesis_after_post_content', 'wpsites_npp_navigation_links', 5 );

    • Permalink to comment#

      Thank YOU @Brad Dalton !

      Precisely what I was in search for – I have scoured the entire WP codex / forums and – nothing !

      You ROCK \m/ ! :)

    • Permalink to comment#

      There’s actually a better way to do this now if you’re running Genesis 2.0 HTML 5 because it adds classes for styling both previous and next post links.

      The hooks have also changed so you will need to use the 3rd parameter for positioning if running a HTML 5 enabled child theme on Genesis.

    • Permalink to comment#

      Actually, Im not using Genesis @Brad – I simply needed to create a Next & Previous post type links without the actual text because it just didnt look good on the particular area of the page – I simply needed the left & right double angle quotes which look GREAT now :)

      I kept trying all different type of pagination functions – and all that kept occurring was that my theme kept breaking – I was about to give up until I came across your great strip of code here :)

      ( will def give you a link-back to your site once I’m done w my site )

      Thank You once again Brad :)

  31. I actually wrote a plugin that “seems” to work for me.

    https://github.com/adampatterson/nextpost

    Let me know if you have success of fail, it should give you next / previous links inside a single custom post, I don’t think It will work in custom taxonomy but let me know :)

  32. Mrtz

    Thank you : )
    worked for me

  33. Fantastic! it worked right away. But what about if you don’t have any post available yet? Perhaps you can use:
    if ($wp_query->have_posts()) : while ($wp_query->have_posts()) : $wp_query->the_post();
    instead of
    while ($wp_query->have_posts()) : $wp_query->the_post();
    so that we can add “else” statement to show Error message.
    This is what I am using:
    <?php
    $temp = $wp_query;
    $wp_query = null;
    $wp_query = new WP_Query();
    $wp_query->query('showposts=6&post_type=snippets'.'&paged='.$paged);
    if ($wp_query->have_posts()) : while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
    <div class="entry">
    <h2 class="title"><?php the_title(); ?></h2>
    <?php the_excerpt(); ?>
    </div>
    <?php endwhile; ?>
    <div class="navigation">
    <div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
    <div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
    </div>
    <?php $wp_query = null;
    $wp_query = $temp;
    ?>
    <?php else : ?>
    <h2>Not Found</h2>
    <p>Sorry, but you are looking for something that isn't here.</p>
    <?php endif; ?>
    Worked like charm. Thanks.

    • Permalink to comment#

      This worked perfectly for me! The code Chris originally posted didn’t work for me for some reason. But this fixed it somehow so thank you so much!! I’ve been trying to solve this for days!
      Something else I learned: this won’t work if your page-slug is the same as your custom post type name. Just in case that helps others!
      Thanks again!

  34. Andy
    Permalink to comment#

    I am creating a plugin using Custom Post Types… Should Chris’s Snippet include:
    global $wp_query;
    $temp = $wp_query;

  35. A lot if issues for why the pagination doesn’t work has to do with how wordpress sets it’s functions. If you don’t use the loop it seems that mist functions won’t with with custom post types.

    When I was trying to make the next post work I did not have the functions within the loop on the single template.

  36. I ended up using this:

    <?php // this is the current CPT working with NAVI!!!
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

    $args = array('posts_per_page' => 9,'post_type' => 'reviews', 'paged' => $paged );

    $wp_query = new WP_Query($args);

    if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post();
    ?>

    html here ////////////

    <?php endwhile; endif; ?>

    <?php if (function_exists('page_navi')) { // if expirimental feature is active ?>

    <?php page_navi(); // use the page navi function ?>

    <?php } else { // if it is disabled, display regular wp prev & next links ?>

    <nav class="wp-prev-next">
    <div class="page-nav">
    <div class="nav-previous"><?php next_posts_link('&lt;&lt; Next'); ?></div>
    <div class="nav-next"><?php previous_posts_link('Back &gt;&gt;'); ?></div>
    </div>
    </nav>

    <?php } ?>

    <?php
    wp_reset_query();
    ?>

    The page_navi function is part of the 320 Foundation theme which could be omitted and just leave the “else”

  37. Mario Mc
    Permalink to comment#

    There is a easier way to paginate custom post pages without inferring with the main query :

    <?php

    $paged = ( get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;
    $args = array( ‘post_type’ => ‘newsfeed’, ‘posts_per_page’ => 5, ‘paged’ => $paged );
    $newsquery = new WP_Query( $args );
    if ( $newsquery->have_posts() ) :
    while ( $newsquery->have_posts() ) : $newsquery->the_post();

    the_excerpt(); //query here

    global $newsquery; $total_pages = $newsquery->max_num_pages; if ( $total_pages > 1 ) {
    $big = 999999999; // need an unlikely integer

    echo paginate_links( array(
    ‘base’ => str_replace( $big, ‘%#%’, esc_url( get_pagenum_link( $big ) ) ),
    ‘format’ => ‘?paged=%#%’,
    ‘prev_text’ => __(‘ Prev’),
    ‘next_text’ => __(‘Next ‘),
    ‘current’ => max( 1, get_query_var(‘paged’) ),
    ‘total’ => $newsquery->max_num_pages
    ) );

    }
    endif; wp_reset_postdata();
    ?>

  38. Permalink to comment#

    Hello Chris, first of all I am really a big fan of yours and have been following all your updates since I started the web back in 2012. Thanks for your awesome updates.

    Secondly, would you please tell me how are you achieving the syntax highlighting in your blog, I mean is there any wordpress/jquery plugin or what? I am developing my own blog and have to implement such a syntax highlighting.

    Awaiting your kind reply.
    Thanks!

  39. Osu
    Permalink to comment#

    Is it possible to only return posts within a specific custom taxonomy? I’ve created a single-custom_post_type.php template and am using the regular wordpress loop to create my pagination for single (custom) posts, but when I try to return posts within the same taxonomy using next_post_link(), my pagination disappears. You can read more about it here:

    http://wordpress.stackexchange.com/questions/110307/next-post-link-not-working-for-single-custom-post-type-post/110537

    It seems that setting true for in_same_cat in next_post_link('format', 'link', 'in_same_cat', 'excluded_categories') doesn’t work for custom taxonomies, but is there a way to make that work?

  40. Permalink to comment#

    Thanks for the info!
    This wasn’t quite working for me (kept getting the 404s etc.).

    I also found this great post which shows how to add the page numbers (instead of just prev/next).

    http://designphiliconline.blogspot.com.au/2012/08/wordpress-custom-post-type-pagination.html?showComment=1378003312469#c8786213242726004295

  41. Jake
    Permalink to comment#

    First, thanks for this post! Second, I got this code working on my index.php page, however, as I saw mentioned above, page 2 generates a “Page Not Found” in the title. Is there a fix for this?

  42. Mike Harding
    Permalink to comment#

    Will this work on a custom post archive template (ie. archive-artist.php) or is a custom page template (page-artists.php) required?

  43. Permalink to comment#

    Really appreciate the link. Thanks Mia

    I’ve got over 1000 code snippets on my site i know that work because i test all of them.

    • Permalink to comment#

      Im THERE ! soaking it all up like a sponge Obi Wan :D

      bookmarking your PastebinGithub & WP Sites

      ( between you & Chris Coyier – I could become a Rocket Scientist by this time next year ! :D )

      Thank You BOTH – its always a privilege learning from great minds :)

  44. Permalink to comment#

    Hey there..
    I am using this code in my home page and when I go to page 2 (../page/2) it returns the 404 page.
    Can anyone help?

  45. Autumn
    Permalink to comment#

    I’ve been looking all over for days for code that worked for me!
    If I could even get it to paginate it was showing the same posts and I tried everything I could find.
    This worked. Thank you!

  46. Kentus_
    Permalink to comment#

    You absolute rockstar. You just saved me more hours!

    Thanks you :)

  47. Thank you Chris! Words cannot describe the magnitude of your awesomeness!

  48. Permalink to comment#

    There’s an issue with this method. My scenario in which this occured:
    – The blog part of the site displays 5 posts per page at most.
    – The blog has 9 posts currently.
    – My portfolio displays 2 posts per page at the most.
    – The portfolio has 8 posts currently.

    The blog thus needs two pages to display all posts, but the portfolio post type needs 4. Problem: there is no page 3 or 4. I.e. /portfolio/page/2/ works fine, but /3/ doesn’t exist. And it makes perfect sense too; WP doesn’t know what to do when it hits /3/ since the math being done would suggest that /page/2/ would be sufficient (since it is if it’s using the ‘5’ setting under “Reading”.

    Either I’ve done the implementation wrong (I’d welcome a solution…) or this method is very flawed.

    I believe this is the same problem that @gbarchik has.

  49. Thank you dude, your code works fine.

  50. Manish khatri
    Permalink to comment#

    Nice code, works perfectly. thanks for help me.

  51. Pete
    Permalink to comment#

    You have to use the var $wp_query!

    Argh! I could not understand why the posts were displayed but next_posts_link() was empty. I was using a made-up var $post_query instead of $wp_query. Know I know. And this may help someone else.

  52. Chris Montgomery
    Permalink to comment#

    showposts has been deprecated for a very long time. posts_per_page should be used instead.

  53. You can improve this to a prettier numbered structure too by using WordPress’s function paginate_links. I wrote a full tutorial here on it.

  54. Sam Mathew
    Permalink to comment#

    How to filter it by taxonomi filter ?
    query(‘showposts=8&post_type=portolio&filter=free’.’&paged=’.$paged);

    while ($wp_query->have_posts()) : $wp_query->the_post();
    ?>
    Above code is not working at all . Please help.

  55. Rhon
    Permalink to comment#

    Thanks Man, You save the Day for this code :) … But I want to have a Numeric Pagination using the code you provide.. Is it possible?

    More Power to you Buddy :D

  56. Worked like a charm. Needed the &paged=’.$paged part to complete my code.

    Thanks

  57. Za
    Permalink to comment#

    Hey, it works. I’ve been stuck in this problem for quite a while. Thank you for providing the code. Thank you so much.

Leave a Comment

Posting Code

Markdown is supported in the comment area, so you can write inline code in backticks like `this` or multiline blocks of code in in triple backtick fences like ```this```. You don't need to escape code in backticks, Markdown does that for you.

Sadly, it's kind of broken. WordPress only accepts a subset of HTML in comments, which makes sense, because certainly some HTML can't be allowed, like <script> tags. But this stripping happens before the comment is processed by Markdown (via Jetpack). It seems to me that would be reversed, because after Markdown processes code in backticks, it's escaped, thus safe. If you think you can fix this issue, get in touch!

If you need to make sure the code (typically HTML) you post absolutely posts correctly, escape it and put it within <pre><code> tags.

Current ye@r *

*May or may not contain any actual "CSS" or "Tricks".