Last updated August 5th, 2022 at 04:22 pm
This article shows you how you can use a simple, lightweight plugin to make it easier for your Web visitors to find what they’re looking for in your blog. The plugin, Search & Filter, is one I installed long ago, deactivated because I hadn’t followed directections, and then forgot about — until just the other day.
And isn’t making our visitors’ experiences better what it’s all about?
Preface: How I Structure Blog Posts
In the Web sites I build, and especially in my own, I try hard to utilize taxonomies (usually Categories and Tags) to organize blog articles in ways that make it easy for visitors to find what they’re looking for.
Taxonomies are nothing more than ways to group content.
I tend to use categories as broad, mutually exclusive classifications. In other words, I assign only one category to each blog post. Also, I try to use only a few catetories. The current categories in this blog are:
- Case Studies
- First Looks
As for tags, I use them in a “many-to-many” fashion, and I use a lot of them. Tags are topics (in the case of this blog, CSS, WordPress, php, etc.). Each blog post could be assigned multiple tags, and any given tag could (actually should, or else you’re doing it wrong) be assigned to multiple posts.
As I wrote in this blog post from years ago, I think of categories as the chapters in a book’s table of contents and tags as the entries in a book’s index.
How Visitors Use Categories and Tags
Many blog readers know they can view a listing (archive, in WordPress parlance) of posts in a certain category or assigned a certain tag by clicking on the category/tag links that typically appear at the top or bottom of a blog post.
I do this all the time when reading others’ blogs. It’s potentially a helpful time-saver.
But I’ve often wished for something better than having to choose between the category archive and the tag archive.
Suppose I were a visitor to this blog and I’m pressed for time and I’m particularly interested in finding useful php snippets. Maybe I’ve visited before and found some useful ones, and I want to see if any new php snippets have been added.
By clicking on “php” in the tag cloud or at the bottom of a post, I’d see a listing of all posts tagged “php”. But that listing would include posts in all categories.
Sure, I could navigate through all the archive pages and scan for the php snippets posts. But that isn’t ideal.
Yesterday I discovered in one of my sites a plugin I had deactivated months ago because it couldn’t do — or so I thought — something I wanted it to do.
The plugin is Search & Filter, and it turns out the problem was due to user error.
Search & Filter purports to be an improvement over the standard WordPress search box. This, from the description at the WordPress repository (emphasis mine):
It was the part about using the plugin as a filtering system that caught my eye and interest.
There is a bug in version 1.2.10 of the plugin, which can be fixed in 20 seconds by anyone who knows how to edit and FTP a php file (see below). The plugin author announced 16 months ago that he plans to update the plugin (which he has not done) and that he will not offer support in the meantime (also which he has not done). Not wonderful.
The Bug and How to Fix
Depending on the version of php your site uses, you may get this error:
Warning: Invalid argument supplied for foreach() in /home/[your_site]/public_html/wp-content/plugins/search-filter/search-filter.php on line 578
…and then other messages about “Headers already sent”.
Here’s the fix, thanks to @coffeehero in the plugin support forum:
Find this on line 578 of search-filter.php…
foreach($wp_query->query as $key=>$val)
…and change it to..
foreach((array) $wp_query->query as $key=>$val)
How to Filter Blog Posts with the Search & Filter Plugin
Installation is standard, and the plugin works by shortcode. I don’t need to tell you how the shortcode works, because all the documentation is here.
To achieve the filtering I’m looking for, I don’t even include the search box. If someone wants to search for a string of text, he/she can use the built-in search form, as far as I’m concerned. Remember, the problem I want to solve is helping someone find posts with a specific tag in a specific category — quickly and easily.
So, whether you’re looking for php (tag) Snippets (category), or WordPress (tag) Case Studies (category), or CSS (tag) Techniques (catetory), here’s the search form you can use (go ahead and try):
And here’s the shortcode:
[[searchandfilter fields="category,post_tag" hide_empty="1,1" show_count="0,1"]]
If you want to make it easier for your Web visitors to find what they’re looking for in your blog (and why wouldn’t you?), consider installing this lightweight (173 KB) plugin. Notwithstanding the above-referenced bug and lack of support, it’s a nifty solution.
By the way, remember that the plugin also supports custom post types, custom taxonomies, and post dates. You might want to use a more full-blown search form than I’m using.
Note: There’s a Pro version of the plugin which supports dynamic filtering, which prevents users from getting empty results. In other words, using my example, after selecting one category, the plugin will dynamically filter the tags that appear in the Tag dropdown, removing any tags not associated with the selected category and updating the counts of those that do. (Dynamic filtering also works in the other direction, if you select Tag first.) However, when I tested the demo for the pro version, I found the dynamic refresh too sluggish. YMMV.