NOTE: An update to this tutorial now lives over on VoodooPress. It’s got the same code examples, slightly cleaned up. But the explanation is a little more lengthy, in case you want to understand what is going on.
Here’s something I run across frequently on the WordPress Forums. Pagination not properly working on custom pages using query_posts. Well, the solution is quite simple. You have to account for pagination anytime you use a custom query. And you do that in the query_posts line. Here’s an example of how that is done.
First off, custom queries are good for calling or excluding certain posts to a page. Lets say we want to display 5 posts per page from category 10. The best place to learn how to do this is the Codex. You would probably start after reading that article like this:
<?php query_posts('posts_per_page=5&cat=10'); ?>
Now technically this would be proper, and it would get you the results you are after, but there would be no pagination. So if you had many posts and were trying to go to the next page, the same posts would still show. Here’s how to fix that. First off, I like to put all my args for query_posts into an array, it’s so much simpler to look at and organize. So the above as an array would be:
<?php query_posts( array ( Â Â Â Â 'posts_per_page' => 5, Â Â Â Â 'cat' => '10', )); ?>
And that would work the same. But it’s more organized to me, and so much easier to add more items. Now  the answer to our problem. We will take that above query array and get pagination working.
<?php query_posts( array( Â Â Â Â 'posts_per_page' => 5, Â Â Â 'cat' => '10', Â Â Â Â 'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ), )); ?>
And there we have it! I think that’s pretty simple. Always include pagination within your custom query and you’ll be good to go!
UPDATE: Thanks to a reader, and a little scan through the codex, it looks like we may need to update the code above.
<?php query_posts( array( Â Â Â Â 'posts_per_page' => 5, Â Â Â 'cat' => '10', Â Â Â Â 'paged' => ( get_query_var('page') ? get_query_var('page') : 1 ), )); ?>
Looks like the way to do things since WP version 3.0.2. You can read about it in the codex. All we did was change the get_query_var from paged, to page.
Thanks so much. You saved my bacon.
I found that using page_navi plugin that I also had to use after the loop to make its pagination kick in.
Using a neat “order by date” / meta_value as well… (date format must be yyyymmdd)
query_posts (array (
'orderby' => meta_value,
'meta_key' => date2,
'order' => ASC,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ),
'posts_per_page' => 16,
));
}
Thanks for taking the time to comment, and I’m glad you found this helpful!! I only recently started having the need to use custom queries, and this was a lesson learned the hard way!
Hi!
I’m going crazy with this pagination thing. Have tried several approches now and can’t quite get it right.
I’m using this code (with your example), but when I click on next page it displays the exact same posts, but in the URL it shows /page/2
4, 'posts_per_page' => 5,'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 )));
$count = 0; if(have_posts()):while(have_posts()):the_post();?>
Any thoughts on why?
I do have multiple loops and also a $count variable in this page template.
Thanks!
Hm, some code got stripped there.
4,
Your code still got stripped out. You could drop it in a pastebin and report the link
Also, multiple loops, do you reset them properly?
Yeah, sorry about that!
http://pastebin.com/shktGJGT – there’s the complete code. I hope it’s not to messy for you to figure out..
Thanks!
http://codex.wordpress.org/Function_Reference/WP_Query#Pagination_Parameters
I wonder if this applies? The last bit of the pagination section discussed using page rather than paged in get_query_var. Other than that, I’m looking over your code, but I’m not super familiar with multiple loops…. definitely something I need to play with a bit….
Major kudos! Thank you so much!
Absolutely! I’m glad you found this useful!!
Hi Rev. great article, thanks!
There is a typo in the first line of code, double ampersand: posts_per_page=5&&cat=10
Yup, sure is! Thank you, all fixed!
I’ve been searching for 10 hours now to solve my pagination problem and I was hoping you can help. I’m using a Custom Select Query to return posts sorted by Custom Field value and it’s working but it returns all my 350 posts in one page I need to add a pagination to only show 20 posts per page like the rest of my website.
Here is the code that I’m currently using :
http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query
I really hope you can help me on this.
http://wordpress.org/support/topic/custom-select-query-w-pagination?replies=11
See if anything in there helps you out? I’ve never wordked with DB queries, so it’s not my strength, but I’m always happy to learn something new
Was really hoping this would solve my issue but it didn’t. Can you take a look at this an offer any suggestions?
No matter how I manipulate the code, I always get exactly two pages, yet there are 23 posts. I have output to screen the $wp_query var and verified that it is finding all 23, the SQL call looks correct, etc. But my navigation will only show two pages. Period. I can modify the URL to see page 3, 4, etc and they look fine (except pagination then says page 3 of 2).
Thoughts?
global $wp_query;
query_posts(
array_merge(
array(
‘orderby’ => ‘date’,
‘order’ => ‘DESC’,
‘paged’ => ( get_query_var(‘paged’) ? get_query_var(‘paged’) : 1 ),
‘posts_per_page’ => 4
),
$wp_query->query
)
);
I’ll be honest, queries aren’t my thing…. how are you running this? Is it coming from functions.php or in a template? Just trying to figure out the glbal and array merge business. Does it work as a simple query directly in the template?
‘paged’ => ( get_query_var(‘paged’) ? get_query_var(‘paged’) : 1 ),
solved my problem
thanks
I’m glad that worked for you!!
Thanks a lot!!! :-)))
Sure! Glad it helped you!
Thank you so much for sharing your code with us. I’ve searched for hours for a solution. Your code did the trick! You made my day!
Awesome! I’m super glad this helped you!
You have just saved my ASS dude!
Nice! I’m glad I could help you out!
dude you fucking geniuos!! I had a big problem with pagination, when I clicked ‘Older Posts’ it redirected me to the page 2 but it showed exactly the same posts as in home page, but your code solved it. Thanks a million!!
I just made a little change and it now it works fine for me:
5,
'cat'=>'1',
'paged' => ( get_query_var('page') ? get_query_var('page') : 1 ),
));
?>
instead of ‘paged’ i put ‘page’ because I found this (http://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters)
Show Posts from Current Page
Display posts from current page:
$query = new WP_Query( ‘paged=’ . get_query_var( ‘page’ ) );
Pagination Note: You should set get_query_var( ‘page’ ); if you want your query to work with pagination. Since WordPress 3.0.2, you do get_query_var( ‘page’ ) instead of get_query_var( ‘paged’ ). The pagination parameter ‘paged’ for WP_Query() remains the same.
so yeah, problem solved!
Oh cool, I’m glad you let me know about the change! I’ll have to update this post and/or write a new on over on VoodooPress.com to reflect that info. I don’t have any sites running custom queries right now, so I hadn’t had to work through the change. Thanks, and I’m glad my article helped you out!
cheers it solved problem of pagination and no of with posts with my wp custom template. thanks for help.
I’m glad that helped you out!
Hey Rev. Voodoo, I have been searching for a while and think you may be the one to help. I have this code:
<?php $paged = (get_query_var('page')) ? get_query_var('page') : 1; query_posts("cat=3,4,9,11,16&showposts=10&page=$paged"); while ( have_posts() ) : the_post() ?
The thing is it shows the entire post on the blog page, is there a way to make it sow only a summeary or excerpt?
I certainly can help! To display an axerpt is fairly simple! But it has nothing to do with query_posts per se. Here’s how things work… query_posts, or get_posts, or any of those calls set your query. That basically gives WordPress instructions on what you are going to want to display. Then we have the loop. The loop is the portion of your template that actually displays what you want based on your query. It’s called a loop, because it loops over and over displaying posts based on your query, it loops a certain number of times based on either the setting you have in general->reading, or if you set a posts_per_page parameter in your query.
So, since displaying a full post, or an excerpt is not a query parameter, but rather a loop thing, take a peek at your loop. You have the beginning of your loop above. It starts at the while ( have_posts()): the_post() line you’ve displayed above, and it ends at the endwhile command. Everything in between is your loop, the portion controlling how your post looks.
Since you are displaying your complete post, I’m betting that somewhere in your loop you will find a call to the_content.
http://codex.wordpress.org/Function_Reference/the_content
To make an excerpt, you have a couple of ways. First, you can make a custom one for each post, using the custom excerpt box on the add post screen. It shows up under the normal post text box. If you don’t see it, turn on the excerpt in screen options menu on the add post screen. This is useful for people who want to handcraft an excerpt, to tease people into clicking to the full article. Now of course we can set up an automatic excerpt too. That is as simple as finding the_content we talked about earlier, and swapping it to the_excerpt.
http://codex.wordpress.org/Function_Reference/the_excerpt
That will automatically grab the first 55 words of your post and display it. That can be adjusted, like pretty much anything else in WP.
Added to your functions.php file would change the excerpt from 55 to 40 words.
That’s hopefully a good start for you! Other things are possible with the excerpt, like building a function to allow you to choose a word length per template or to swap out the … at the end of an excerpt for something else, but hopefully I’ve got you headed where you wanna go!
That did it! Thank you, so simple when you know, huh? YOU are totally amazing!
OK, this is my next thing, i looked up styling the excerpt and putting the
function new_excerpt_more($more) {
global $post;
return ‘ID) . ‘”>Read the Rest…‘;
}
add_filter(‘excerpt_more’, ‘new_excerpt_more’);
in the functions.php file, but where do i put it? doesn’t it need to be inside “?>”
and finally, is there a way to put the thumbnail with the excerpt?
to post code in your comment here, you have to wrap it in language tags with square brackets. like
[ php ]
your code
[ /php ]
But without the spaces in between the brackets and the php. That keeps your code from being eaten by my site!
Anyway, the code you have above goes anywhere in functions.php. It doesn’t need it’s own php tags. The way your functions.php file should be layed out, is an opening php tag at the very top, and maybe a closing one at the bottom of the file (the closing one isn’t needed, I never have one. Within that set of opening and closing tags go all your functions. Never put any code above or below those.
So your file should look like:
Make sense?
As for the thumbnail, all you have to do is call it yourself. This is where some work may have to happen. actually calling the thumbnail is easy
That’s the basics. But exactly where in your loop you call the thumbnail depends on where it will be positioned. You may also need to use some css to get it laid out exactly how you want.
And finally, you can call to certain size thumbnails, or you may register a custom size and want to call that.
Thanks so much for your help. I got it to work, with the thumbnail and it looks just like i want it to. You are awesome, thanks so much again.
I’m glad it worked, and you’ve got everything how you want it!!
Thanks man. Finally i found the answer to my problem.
This was the only article that helped me figure out my problem! Lots of articles were looking at “paged” instead of “page”. Finally fixed!! Thank you so much!
I’m glad we were able to get you helped out!!
tanks for this cods but
i use wp_paginate plugin for wp pagination and use query_posts for list my post that have a custom field …
but when list my post in pagination ; page number is number of all post and show 404 pages in last number…
you see my problem in index page in http://testdl.ir….
if i can slove this problem, comment here…
tnx
I have the same problem but your solution doesn’t work with me. I still have my broken link /page/2
Have you an idea ?
There is my code :
global $wp_query;
query_posts( array_merge ( array(
‘posts_per_page’ => 5,
‘cat’ => $catencours,
‘paged’ => ( get_query_var(‘page’) ? get_query_var(‘page’) : 1 ),)
));
if ( have_posts() ) while ( have_posts() ) : the_post();
Thank you so much.
hi bro, thanks for such a useful post and i just want to inform you that, i am using latest version of wordpress something 3.9.xx
and this line ‘paged’ => ( get_query_var(‘page’) ? get_query_var(‘page’) : 1 ),
didnt worked for me instead ‘paged’ => ( get_query_var(‘paged’) ? get_query_var(‘paged’) : 1 ),
worked for me…
thanks and please update!
How I can get total count, I need to perform an ajax pagination
PD: I am new…maybe my question is quite silly
THANK MAN
I think this tutorial about WordPress pagination help you https://www.wpblog.com/use-wp_query-to-create-pagination/