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.

Three

This is my latest album. It is called Three. I’ve just made it available for free streaming and download over on my music site.

Warning: here there be dragons. Well, not really dragons so much. I would classify the album as progressive rock, but not the wizards and sorcery kind of prog rock. Still, if you’re not in the mood for 20-minute rock suites or free-form improvisation, it may not be your bag, baby. There’s Mellotron. A lot of Mellotron. Never fear, there’s also a long essay describing the album’s creation in copious detail for your insomnia-curing pleasure. Enjoy!

(Note: CDs are on their way in the next week or so…)

My Favorite Pomplamoose

I was delighted by what Pomplamoose did with their cover of “Single Ladies” (mentioned in my previous post). But I am in utter, slack-jawed awe at their distinctively twisted cover of “My Favorite Things.”

This is a song I know well, having devoted my undergraduate thesis to the evolution of John Coltrane’s treatment of it over the years. Pomplamoose has taken the song in a completely different, but no less unexpected direction. It’s absolutely wonderful. And now I need to go cry in the shower, Tobias Fünke style, because I will never in my wildest dreams be able to produce something this amazing in my own home music studio.

Get your Schrute Farms Beets gear here!

Update November 18, 2007: I withdrew this post a few weeks ago, after receiving an email from CafePress notifying me that they had received a cease and desist notice from NBC/Universal’s lawyers regarding the huge number of CafePress shops that were selling products that infringed upon NBC’s intellectual property rights to every word uttered in an episode of The Office — or for that matter, every thought that has ever passed through the minds of the show’s writers. Or something like that. At any rate, CafePress had already summarily removed all “Schrute Farms Beets” items from my store. I don’t blame them; it’s just lame that NBC is taking this approach. Of course, that’s partly because NBC is selling their own Schrute Farms Beets shirts, which naturally are more accurate to the one Dwight wore in the episode. (Mine wasn’t quite homemade-looking enough.) So if you’re looking for a Schrute Farms Beets shirt, by all means buy the official product. But if, on the other hand, you are interested in one of my other stupid original designs (the sliver of hope of which is what inspired me to reinstate this post), read on.

Schrute Farms Beets long-sleeve t-shirtAfter last night’s uproarious season premiere of The Office, I couldn’t resist the temptation to jump on the unofficial merch bandwagon with the 2000+ other Office-inspired items available on CafePress, mainly because no one else seems yet to have nailed the cheap, homemade, stenciled look of Dwight’s “Schrute Farms Beets” shirt. I didn’t totally nail it either, without a perfectly accurate stencil font at my disposal, but this is at least a lot closer than what else I’ve seen out there. (A lot of people have come up with very elaborate and well-designed logos for the Schrute Family Farm, but they seem to have missed the point. Last night’s episode demonstrates that if the Schrutes did have shirts, they would only sport the most rudimentary of designs.)

And so, my offering. I’ve attempted to recreate the “bleed-over” look of a painted-on stencil, since that appears to be how the actual shirt Rainn Wilson was wearing was made. Three styles of shirts are available now in the Room 34 Online Store with this design.

But Wait! There’s More!

I went a bit crazy with the designs tonight. Here are a few more that are also available now! (Click on any for a closer look. Then click here to buy one! You know you want to!)

Old OLD School. Tha 507, representing Southern Minnesota Seven Days without Pizza Makes One Weak!