WordPress Search Hacks

WordPress Search Hacks

Display the number of results in WordPress search

WordPress users know it: Search is definitely one of WordPress weak point. Today, let’s see how to get the number of results the search returned, and display it (proudly!) on our blog.

Open your search template file, search.php. In it, search for the following:

Search Results {.pagetitle}

Now replace this with:

Search Results for < ?php /* Search Count */ $allsearch = &new WP_Query(“s=$s&showposts=-1”); $key = wp_specialchars($s, 1); $count = $allsearch->post_count; _e(”); _e(‘‘); echo $key; _e(‘‘); _e(‘ — ‘); echo $count . ‘ ‘; _e(‘articles’); wp_reset_query(); ?> {.pagetitle}

That’s all, you’re done!

Highlight the Search Terms in Results

When you search on Google or any other major search engine, the words you searched for are highlighted in the results to make it easier for you to scan. We can do the same with your search results page.

In search.php, search for the following:

< ?php the_title(); ?>

This is the code for showing the title of each of the results. In the default theme, it will be wrapped up in a line like this: