Using Livesearch for Tags

Anyone who understands tagging also understands the benefit of being able to search for posts by tag. If you’ve ever used Technorati to search by tag, then you know how much more accurate your results tend to be. Folksonomy has become a huge part of blogging lately, but the whole purpose behind it gets a bit lost in the fact that not very many blogs with tags have a way to search for posts by tag. And isn’t that the whole point behind tagging our posts–to make them easier to find?

The advent of the Ultimate Tag Warrior plugin for WordPress has made it possible to use Livesearch to search for posts by tag rather than by content. By making just a few modifications to your livesearch files and search form, you can very easily repurpose Livesearch to search for posts by tag. The Ultimate Tag Warrior adds a “tag” parameter to your index.php file. Using this parameter in place of the traditional search parameter, “s,” changes the query type from index.php?s={search pattern} to index.php?tag={search pattern}.

There are only three files that need to be modified, and you won’t believe how simple it is. The files that need to be modified are livesearch.js.php, livesearch.php and searchform.php.

Starting with searchform.php, the following changes should be made:

Change
$search_text = “$s”; to
$search_text = “$tag”;

and

name=”s” to
name=”tag”

and that’s it for searchform.php.

Then you should edit livesearch.js.php and make the following changes:

Change every occurance of

document.forms.searchform.s.value to
document.forms.searchform.tag.value

and then change the line of code that reads

livesearch.php?s= to
livesearch.php?tag=.

Finally, edit livesearch.php and make the following changes:

Change both occurances of
$posts = query_posts(‘posts_per_page=-1&s=’.$s.’&what_to_show=posts’); to
$posts = query_posts(‘posts_per_page=-1&tag=’.$tag.’&what_to_show=posts’);

That should do it. For your convenience I’ve included a zip file of those three files so you can just replace the ones in your theme. However, do yourself (and me) a huge favor and backup your files first. To see it in action, try out the livesearch on this site.

Using Livesearch for Tags

Leave a Reply

Your email address will not be published. Required fields are marked *