Add Custom Post Types to Omnisearch

The Omnisearch module within the Jetpack plugin for WordPress seems like a dream come true. One search to rule them all!

Amazing, right?

Wrong! Omnisearch only searches posts, pages, comments, media, and plugins by default and has no settings to add custom post types. I set out on a quest to add custom post types to Omnisearch. I found indicators along the way confirming it was possible (such as Cole Geissinger’s helpful article from 2013). I also found this official looking code snippet for adding results to Omnisearch. I even found this Jetpack support thread tying them both together and leading me to believe I was on the right track.

The short of it is that it didn’t work. My best guess is that these code snippets worked on prior versions of Jetpack or that perhaps key elements were missing from the code examples. So the hunt was on!

Luckily since I already had the class name from Cole’s code, it didn’t take too long once I dug into Jetpack’s code to find the solution. By using theĀ omnisearch_add_providers hook within Jetpack, it is actually a very simple matter to add as many custom post types as like. No other code is needed to augment the results as this code will result in the new custom post types being added automatically to the Omnisearch results.

Just edit this code snippet to reflect your custom post types (use the slugs!), replace the “xyz_” in the function name with something unique to your site, and place this code into your theme’s functions.php file.

What do you think?