<?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('« Newer') ?>
<?php next_posts_link('Older »') ?>
</nav>
<?php
$wp_query = null;
$wp_query = $temp; // Reset
?>
Paginate Custom Post Types
Chris Coyier
on
(Updated on )
Something went wrong. Missing code tags?
Fantastic, that worked fine!
Missing code here? I really want this!!
Go and study this:
http://wordpress.org/support/topic/custom-post-type-taxonomy-pagination
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.
It’s ridiculous that it hasn’t been updated or at least pulled down if 90% of the code is missing.
fixed.
This don’t work!
yes it does :-)
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.
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!
Thanks, Worked like charm!! :)
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.
Did you ever get this working? I am having the same problem and would appreciate any insight. Thanks in advance.
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.
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
Update URL: Custom Post type pagination
Super awesome thank you Tirumal, that is sweet. Saved me so much time.
I will be using this code many times in the future.
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!
In this line
how i do include several post types?
fox example
$wp_query->query(
=> ‘6’,array(
'posts_per_page
‘post_type’ => array(
‘books’,
‘movies’,
‘links’
),
‘paged’ => $paged
)
)`
Thanks for this post, it was very helpful!
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
Hero Simon! Thanks! :D
Hi, i have lost 4 hours but with his code ITS WORK !
Thanks a lot friend !
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 :)
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?
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.
Thanks, Abel!
Thanks Able, that’s what I needed.
Dont work with offset
With offset all pages have first post.
Now work xD
Sigh, this doesn’t work when using it on index.php :( Can’t seem to get anything working
Hi Amber
Abel Cabans code a few comments up worked for me on index.php
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
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();
?>
Derek,
Try this:
Solution of Paging for Custom Posts Type
Hopefully, it will help you.
Ctrl+c
Ctrl+v
Works awesome. Thanks!
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?
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?
@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();
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?
Is the reset used above
$wp_query = null;
$wp_query = $temp; // Reset
better, or the same as
wp_reset_postdata();
thanks
Here’s a weird one if I change the query variable to $loop it doesn’t work, but if its $wp_query it does????
That is because of $wp_query being a global variable that is used in many other functions, including those to do with pagination. Thus when you overwrite $wp_query you’re changing the value that the other function will use – otherwise you’re just changing your private little loop variable that isn’t referenced elsewhere.
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
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??
Cheers Chris, worked a treat!
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;
}
}
function wpsites_npp_navigation_links() {
if( 'portfolio' == get_post_type() ) {?>
<?php previous_post('« « %', 'Previous', 'no'); ?>
| <?php next_post('% » » ', 'Next', 'no'); ?>
<?php
} }
add_action('genesis_after_post_content', 'wpsites_npp_navigation_links', 5 );
Thank YOU @Brad Dalton !
Precisely what I was in search for – I have scoured the entire WP codex / forums and – nothing !
You ROCK \m/ ! :)
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.
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 :)
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 :)
Thank you : )
worked for me
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('« Older Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Newer Entries »') ?></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.
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!
I am creating a plugin using Custom Post Types… Should Chris’s Snippet include:
global $wp_query;
$temp = $wp_query;
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.
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('<< Next'); ?></div>
<div class="nav-next"><?php previous_posts_link('Back >>'); ?></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”
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();
?>
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!
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
forin_same_cat
innext_post_link('format', 'link', 'in_same_cat', 'excluded_categories')
doesn’t work for custom taxonomies, but is there a way to make that work?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
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?
Will this work on a custom post archive template (ie. archive-artist.php) or is a custom page template (page-artists.php) required?
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.
Im THERE ! soaking it all up like a sponge Obi Wan :D
bookmarking your Pastebin – Github & 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 :)
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?
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!
You absolute rockstar. You just saved me more hours!
Thanks you :)
Thank you Chris! Words cannot describe the magnitude of your awesomeness!
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.
Thank you dude, your code works fine.
Nice code, works perfectly. thanks for help me.
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.
showposts
has been deprecated for a very long time.posts_per_page
should be used instead.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.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.
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
Worked like a charm. Needed the &paged=’.$paged part to complete my code.
Thanks
When I click on the “2” to go to page2 of the archive page, it goes to index.php file template. Do you know how to fix this?
Did you solve this?
To help others, I had the same problem for a custom post type ‘news’. I had to change my news index page URL from /news to /latest-news to solve the problem.
Awesome! Worked for me! Thank you so much! CSS Tricks does it again!
Work Great! Thanks!
Thanks for this, it saved my day!
Awesome! I was looking for this one :)
Thank You.
I use this code for WordPress custom loop with pagination:
Source: http://web-profile.com.ua/wordpress/themes/wordpress-custom-loop/
Thank you!
Only your code worked for me after trying many others.
You are the best! Thank you so much!
Thx, this worked for me :)
Thanks, Worked like charm!! :)
I have seen a custom query post code but for the pagination function I have used this code from https://www.wpblog.com/use-wp_query-to-create-pagination/
`
function pagination($pages = ”, $range = 4)
{
$showitems = ($range * 2)+1;
}
`