I couldn't figure out the best way to write the title, so first of all I hope you understood what i was trying to get across. I have a snippet of code on my template page that fires after the wp_query to display a heading as such:

showing x of y results for ...

and the code:

elseif ($size_option&&!$color_option&&!$genre_option) {
echo '<h3>Showing ' . $wp_query->post_count . ' of ' . $wp_query->found_posts . '    results for ' . $size_name . ', all colors, all genres</h3>';}

As such it will show something like - showing 12 of 17 results for ...

However the issue is i have my posts_per_page set to 12. So when i go to page 2 (for a query that results in found_posts being 17), we get - showing 5 of 17 results for ...

Which is just illogical.

My question is how could i get it to show the following for once again, a total of 17 posts:

On page 1 - showing 1-12 of 17 results for...

On page 2 - showing 13-17 of 17 results for...

I have an idea of what i should use, but no idea on how to go about it. Im guessing i need to implement some kind of count, based on the 'paged'=> $paged arg from my wp_query.

Any help would be wondeful. Thank you, once again!


as requested by Johannes, my query :

$wp_query = new WP_Query ( array(
                                'post_type' => 'tshirt',
                                'posts_per_page' => 12,
                                'paged'=> $paged,
                                'colors' => $color_option,
                                'size' => $size_option,
                                'genres' => $genre_option));
  • How do the initial 12 results come about? Does the query have a 'post_per_page=12' parameter? Could you expand the code shown by the actual query? Nov 10, 2011 at 8:18
  • Hey johannes, ive added the query @JohannesPille
    – user10106
    Nov 10, 2011 at 9:05

2 Answers 2


Given your posted query and that you seem to still be trying to figure it out:

First, off you should not use $wp_query = new WP_Query ();, since $wp_query is a global wordpress core object. Use any other variable to store the custom query in, $the_query = new WP_Query (); for instance.

That being said, the below code does what you want (if you did rename the query-storing variable $the_query).

$current_page = $the_query->query_vars['paged'];
$per_page = $the_query->query_vars['posts_per_page'];
$total_posts = intval($the_query->found_posts);
$first_paged_post = (($current_page - 1) * $per_page) + 1;
$last_paged_post = min($current_page * $per_page, $total_posts);

echo '<h3>On page '.$current_page.' - Showing #'.$first_paged_post;
// to account for the case where the last page has only one post (i.e. total of 13, 25) 
if ($last_paged_post != $first_paged_post) {
    echo ' - #'.$last_paged_post;
echo ' of '.$total_posts.' results for '.$size_name.', all colors, all genres</h3>';

Edit, Note: While this is more copy&paste ready, it follows the same basic logic as Geert's snippet already provided.

  • Both yours and Geert's snippet worked, but i had a major issue with the 'paged' vaue. As an example page 1 would have a $current_page of 0, while page 2 would have a $current_page of 2. So the snippet would work properly but only on page two and beyond that. On page one it would give a negative result and zero. However i have fixed it with this for now: if ($current_page==0) { $current_page = 1;} , Any idea why this was happening or how i could fix it properly? Also if i use anything other than $wp_query the pagination stops working, any ideas? @Geert
    – user10106
    Nov 10, 2011 at 13:39
  • nevermind i fixed the problem, with $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; As you can see i'm diving in and learning as i go. Thank you both for all your help!
    – user10106
    Nov 10, 2011 at 13:53
  • @Mike If you don't have another loop on the page and using $wp_query does not break anything, I guess u can keep it that way. Alternatively, see this Q&A for how to paginate a custom query. As far as the value of the paged key is concerned, it being 0 on the first page is expected behavior. I should have accounted for that. I am stumped as to why that's the case and what its benefits are. Yep, your solution (codex does it that way as well) is perfect! Nov 10, 2011 at 14:05

I once calculated these variables for the Kohana Pagination library. If you replace the variables with your own you should be all set.

$current_first_item = (int) min((($current_page - 1) * $items_per_page) + 1, $total_items);
$current_last_item  = (int) min($current_first_item + $items_per_page - 1, $total_items);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.