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('« Newer') ?>
<?php next_posts_link('Older »') ?>
</nav>
<?php
$wp_query = null;
$wp_query = $temp; // Reset
?>
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 :)
//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?
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.
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????
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;
}
}