## x to y of z search results

I’m the dozen hundredth person to code this solution. I am always rebuilding my site on an ongoing basis-see a better way, code a better way. Today I’ve worked on the search results template. I want to be able to sort results, and have a counter for the results in the manner of “results x to y of z”:

It is pretty easy. Three numbers are needed:

1. Current page number.
2. Number of posts displayed per page.
3. Total count of search results.

Calculate the top count-range number first (y in ‘x to y of z’):

`\$page_number * \$posts_per_page;`

Then the bottom count-range number:

`\$count_low  = (\$count_high - \$posts_per_page) + 1;`

And finally round the top count-range if it is over the total:

`\$count_high = (\$count_high > \$total_results) ? \$total_results : \$count_high;`

Here is the completed function. You can just drop this into your functions.php as-is.

```function search_results_count(\$page_num, \$total_results) {
\$page_num = (\$page_num == 0) ? 1 : \$page_num;
\$posts_per_page = get_option('posts_per_page');
\$count_high = \$page_num * \$posts_per_page;
\$count_low  = (\$count_high - \$posts_per_page) + 1;
\$count_high = (\$count_high > \$total_results) ? \$total_results : \$count_high;
echo 'Results ' . \$count_low . ' to ' . \$count_high . ' of ' . \$total_results;
}```

Execute it with this line in search.php:

`<?php global \$wp_query; search_results_count(get_query_var('paged'), \$wp_query->found_posts); ?>`

It will output this string (for example):

`Results 1 to 10 of 158`