WordPress tip: A simple way to search all post types

I love WordPress, but its huge designer/developer community and extensible structure have made it possible to over-engineer a solution to just about every problem. And then under-document that solution.

Case in point: today I needed to add the ability to search across custom post types, along with pages. But by default search only searches posts. (That is, the “post” post type. Are you with me?)

This isn’t a new problem, even to me, although very few of the sites I build have (or need) internal search. It’s just not that useful on a site that doesn’t have hundreds of pages or posts, and most of the sites I build don’t.

In the few times in the past when I needed to be able to search across other post types, or other content like taxonomy data, I’ve relied on the Search Everything plugin. And judging by the fact that (as of today) it’s been downloaded 555,309 times, clearly I am not alone.

It’s a pretty good plugin, as plugins go. But it can be overkill, especially if all you need is the ability to search other post types.

And that’s where we run into the real, multifaceted problem with WordPress for developers: 1) there’s a plugin (no, make that dozens of plugins) for just about every obscure task, and 2) there are also several ways to go about building your own custom solution, especially if you’re building your own theme, but 3) the documentation is all over the place, and none of it is comprehensive.

Granted, by offering a targeted solution to a very specific problem in this blog post, I’m contributing to that documentation fragmentation, but whaddayagonnado.

There’s a fourth (and probably even more important) facet as well: plugins are developed independently by countless individuals (of varying degrees of skill), and it’s impossible for anyone to test them all for interoperability. The more plugins you install — especially if they’re excessively complex for the problem you’re intending to solve — the greater the chance you’ll introduce an incompatibility that will break your site. So it’s in your best interest to try to keep things as simple as possible. (And to err on the side of installing fewer plugins.)

tl;dr

If I understand correctly, every parameter in WP_Query can be passed in the query string, which means you can add corresponding input fields into searchform.php in your theme to modify the search functionality.

OK, now that was too simple (and abstract). Give me an example I can work with.

Here’s a one-line solution to get WordPress to search all of your post types (even custom post types), not just “post”-type posts. Add this into the form in searchform.php:

<input type="hidden" name="post_type" value="any" />

Guess what… you can also specify multiple specific types of posts using PHP’s method of using square brackets in your input name to pass in data as an array, like this:

<input type="hidden" name="post_type[]" value="foo" />
<input type="hidden" name="post_type[]" value="bar" />
<input type="hidden" name="post_type[]" value="baz" />

A caveat: I tried the above with page as one of the values and it didn’t work; it showed my custom post types, but not “page”-type posts. I suspect it’s because that’s one of the predefined type parameters that make the query behave slightly differently. So this solution isn’t perfect, but using any as the value will work: it gets “post”, “page” and your custom post types.

My goal was to have a simple search form that would just search all post types, so I made this a hidden field, but you could make it radio buttons, checkboxes or a select menu if you wanted to let the user pick, and this just scratches the surface of what you can do to customize your search form to leverage the capabilities of WP_Query.

In UX we trust: Netflix as a case study in how good search isn’t good enough

Last night, prompted by a Dan Benjamin tweet, I felt inclined to watch one of my favorite ’80s movies, Fletch. I own Fletch on DVD (two versions, in fact), but I didn’t feel like busting them out. I wanted to watch it on my iPhone in bed, so I decided to check the iTunes Store and Netflix.

Unsurprisingly, iTunes did have it, but only for purchase, not for rental. And I’m not inclined to pay $14.99 to buy a digital copy of a movie I already own twice over on disc. While on iTunes, I saw a recommendation for The Sting. Intrigued, since I have never seen it (gasp!), I considered it as a possible alternative, and was pleased to see iTunes had it as a $3.99 rental. But before dropping four bucks on it I decided to check its availability on Netflix.

I checked the iTunes Store first, because I have learned to assume Netflix won’t have what I want available for instant streaming. Or, more accurately, I have learned not to trust that Netflix will have what I want.

And that’s where the problem lies: I don’t trust Netflix. It’s not that I think they’re up to anything nefarious (it’s not the same as the distrust I have these days for Google, for instance). And it’s not even entirely that I have become jaded by past experience discovering just how woefully limited their selection of streaming content is.

It’s that I don’t trust their interface to really show me what’s available.

Why not? They have a search box, after all. I can just search for what I want. To the best of my knowledge, the search box works pretty well. If they have something, it comes up. If they don’t, it doesn’t.

As usual, I couldn’t keep my frustration to myself, so I took it to Twitter:

So, again, why don’t I trust Netflix? I’ve been pondering that question all morning, and I think I have it figured out. It’s because good search, alone, isn’t good enough. Search is open-ended. It’s also kind of like standing outside a locked door and whispering through the keyhole to someone inside. You know there’s a lot of stuff inside the room. You even know that the room probably holds things you want. But you can’t see for yourself what’s inside, and the person on the other side of the door is only answering yes-or-no questions.

Of course, Netflix does offer more than search. But on mobile devices like the iPhone and iPad, it doesn’t offer a whole lot more. Sure, it has recommendations. And you can browse by genre. But I don’t really give a crap about their “recommendations.” That’s the person behind the door, offering a little bit more information, but it’s far from flinging the door wide open. And browsing by genre is great, if you know what genre the movie is in. If it even fits a genre. (I honestly don’t know what genre I’d find The Sting in. Is it a comedy? Drama? Action? I don’t know enough about the film to find it by genre.) And once you’ve selected a genre, you’re dumped into an experience not unlike rummaging through the cutout bin at a record store. (And if you’re too young to understand that analogy, get off my lawn.)

I took a few screenshots on both the iPhone and iPad, as well as on the Netflix website on my computer, to demonstrate what I’m talking about.

First, search results:

Well, that’s lovely. No results. OK. Did I spell it wrong? Is it case-sensitive? Am I hallucinating and this movie never even existed? Is anybody out there?

OK, well… hmm. What should I do now? Maybe I should browse comedies.

Really… that’s where we start? Can I fine-tune my selection a bit? Sort them into some kind of meaningful order? No, why would I want to do that. I’ll just flip through all of these aimlessly until I find something I can tolerate for the next two hours… I mean no, wait. Help me find what I’m looking for, dammit!

Well, OK. I’ve used enough apps between the iPhone and iPad to understand that the experience is often stripped down on the iPhone due to the smaller screen. That’s understandable. What about if I do the same search on the iPad?

You’re joking, right?

Let’s back up a step and see what Netflix presents to the user when they first enter the “Comedies” genre:

Well… um… that’s… a little better… I guess… than the iPhone experience. This is actually pretty close to what you get when you visit Netflix on the Apple TV, as well, and is somewhat of an improvement — aesthetically, at least — over the old version of Netflix for iPad. But still… it’s just that person behind the door, or the cutout bin.

Open the damn door and let me see for myself what’s in there.

Not that I think this is an adequate solution, let me say that right away, but I decided as a last resort to see if the desktop web interface for Netflix offered a superior experience. Here, where Netflix acknowledges that DVDs do, in fact, exist, the results are a bit better:

Thank you for at least acknowledging that the movie I asked about exists. Thank you for telling me that it’s not available for instant viewing but is available on DVD. Would it be so hard to do that on the mobile apps? I recognize that DVDs are useless on an iPhone or iPad, but simply providing this information reassures the user that their search worked. Now I can move on with my life.

What about browsing? Will you finally just open the door and let me see what’s in the room? At long last, sir, will you please just do that?

Yes!

You may note here that not only am I (after a few extra clicks) able to get a simple, alphabetized list, I am even able to browse subgenres! Who knew such wonders existed?

Sadly, browsing by title within a subgenre is probably the best way to get at what may be an ulterior motive behind the limited browsing interface Netflix presents in its mobile apps, as expressed in my tweet last night: their selection of streaming movies kind of sucks. There are plenty of reasons for this, and I’m not going to criticize Netflix for the challenges involved in working out licensing deals to stream thousands of movies for a very small, flat monthly fee. Netflix is a pretty amazing thing, when you look at what cable TV was like when I was a kid. (What am I saying? Look at what cable TV is like right now!)

Ultimately, though, whether or not Netflix is deliberately hiding its poor selection behind a mediocre browsing interface, it still has a mediocre browsing interface. Who cares? you say. Just search for what you’re looking for. Have you been reading this at all? I reply. Search, no matter how good it is, by itself is not good enough. Users need to be able to get their bearings, see what’s inside the room. We need an understanding of the scope of information we’re dealing with in order to make a meaningful search, and to make sense of the results we’re given, when we can’t find what we’re looking for.

So, a couple of final thoughts on how all of this ranting translates into a meaningful lesson in UX (user experience):

1. Don’t just rely on having a search box as an excuse not to organize and display your content in an easy-to-browse way.

2. Give meaningful feedback when a search fails. Don’t just tell the user “no results.” That’s obvious. Help them out. Give suggestions for alternate searches. And if there’s anything relevant in your database about the user’s search terms, even if it’s not directly available to them in their current context, at least let them know as much.

P.S. As it turns out, Netflix has neither Fletch nor The Sting available for instant viewing. I ended up not watching anything last night, and played around with this synthesizer app instead.

Wow… Ask.com just became a serious contender (again?)

It’s been around for years, but frankly I’ve never given Ask.com much serious attention. There never really seemed to be anything wrong with it; it just wasn’t anything special. So I used Yahoo!… then Hotbot (remember that one?)… then AltaVista (known today only for its notorious offshoot, Babelfish)… then Google. Ah yes, Google… the end all be all of search engines.

Well… maybe.

Check out Ask.com. Not only is it glossed up with Web 2.0 goodness, it actually has some really cool features. Of course the search box has AJAX-based auto-complete. But it’s the results page that’s really impressive. I did a search on John Coltrane (of course… but not solely out of narcissism) and here are the results.

Everything fades in nicely as the results come back. The main column is your typical search results list. The left column gives a bunch of suggestions for narrowing or expanding your search, as well as searching on similar or related topics. The right column is what’s really cool though: different sections feature images, audio, an encyclopedia (well, Wikipedia) entry, and YouTube videos.

Sure, all of that stuff on the right side reeks of “synergistic” partnerships between Ask.com and the source sites. But whether they’re all throwing money at each other or not, the sources are well-chosen, and the overall effect is very cool.

Big thumbs up to Ask.com for their efforts on this. The big question, of course, is how good are the results? I am going to make this my primary search engine for a while, and put it through its paces.

A fix for a fix in WordPress 2.1

One thing that surprised me when I started using WordPress again is that its search function only searches on blog posts, not on static pages. I suppose if most WordPress sites are 99.9% blog posts, then it probably makes sense, but I have enough static pages on my site that I’d like to make searchable to warrant changing this.

Fortunately, someone has. Unfortunately this “Search Pages” plug-in is out of date and no longer works with current versions of WordPress. I dug in a bit and figured out why: the plug-in alters the SQL query that performs the search, but the substring it replaces in the query no longer exists! So I hunted down the place where the query occurs in the WordPress core, adjusted the plug-in as needed, and it works!

Here, then, is my hack of the Search Pages plug-in, modified to work with WordPress 2.1. (I’m not sure which other versions of WordPress it will or will not work with.)

[ search_pages.php.tar.gz ] [ search_pages.php.zip ]

Forget what I said before; we have a winner!

It’s a given that anything I post here is going to be brain-deflatingly stupid. But this one goes beyond even what I would have expected of Microsoft. Fortunately, thanks to a highly effective Google search, I was able to solve the problem in minutes.

The problem was this: For a site I’m working on, we are manipulating the 404 error feature to allow users to set up customized URLs. If the URL entered doesn’t match a real page, it gets fed into this 404 error script, which looks up the path in a database and redirects the user to their customized page. A bit of a hack, but it’s pretty slick.

As usual, I am developing the site using Firefox as my test browser. But… UH-OH! Surprise! When giving a demo of this feature today using Internet Explorer, we discovered it didn’t work! Internet Explorer was not returning the server’s 404 error page, instead using its own internal version (which we’ve all seen and most generally ignore).

Drat! What to do? Well, as it turns out (thanks to the aforementioned Google search), the problem is quite simple. To quote the unbelievable but, as I verified, 100% accurate description I found on this page:

Internet Explorer has a lightly-documented “feature” that stops it from serving any custom 404 error page that is less than 512 bytes long. Your visitors will instead be sent to IE’s own 404 page, which is generic and suggests they use an MSN search to “look for information on the Internet.” That’s one way to lose visitors! Make sure your custom 404 error page is over this limit — about 10 full lines of text and HTML should be enough.

Yep, that’s it. I did my best Bart Simpson-at-the-blackboard impersonation, filling my 404.html file with a large comment block wherein I repeated (about 130 times, for good measure) the phrase “This block exists solely to force Internet Explorer to load this page.”

Sure enough, it worked. D’oh!

Thinking a bit more about this, I at least think I understand why Microsoft chose to do this. “If the server’s default 404 error page is so short,” I imagine them musing, “then it probably doesn’t offer users much helpful information. And since our wonderful web browser is much more important to our users than the web pages themselves, let’s just butt in and do things our way. (Is there really any other way anyway?)”