After you’ve worked with WordPress for any extended amount of time, it’s likely that you’ve worked with post metadata in some capacity. Perhaps you’ve simply read metadata from the database, or maybe you’ve both written to and read from the database table, as well.
It can be a really powerful API whenever you need to associate certain information with your posts (or, if you’re into abstracting things even more, to your models).
Furthermore, assume the use case is just for administrators and it’s only for the administration area of WordPress. That is, you want to search your posts from within the All Posts screen but you want to include the post metadata in the search criteria, as well.
Search Post Metadata
Out of the box, whenever you search for posts in the WordPress administration area, it will look for a phrase in the post title, post excerpt, and post content and then will return those results.
So to expand the search to include post metadata, we have to update the query that’s run such that it incorporates the post metadata tables. But there’s more to it than that. Specifically, for the most flexibility and the best user experience, you’ll want to:
- include portions of a phrase so that it doesn’t just look for something verbatim in the article,
- and, of course, properly handle the case of if no search term is entered so that the All Posts screen can still be viewed in its standard format.
This means we don’t only need to modify the search query to include a meta key and a meta value, but we need to properly escape entire phrases so we can use a
LIKE operation rather than an
IN operation or
Some of the things that are important to know before reading the rest of this article include:
Ultimately, we’re going to need to use two hooks with two custom functions. And because I can’t provide a complete example of what this may look like in your use case, the code has to be a bit generalized.
Hooking Into Search
In the following gist, you’re going to see three things:
- Verification the user is on the administration area, and they are on the edit page.
- The user has entered a search.
- The search phrase has been converted into an array, escaped, and added to the meta query property of the instance of WP_Query.
Assuming all of the above are true, this will update the query that’s run against the WordPress database before the next page renders so that results that include the metadata information.
Updating the WHERE Clause
Before we’re done, though, we also need to make sure that we’re updated the query that’s run.
By default, the query is built using an
AND operation and that prevents us from looking properly through post titles, excerpts, contents, and metadata; however, the query needs to be updated so that it uses an
Quite literally, this means that it will look in the title, content, excerpt, or the metadata.
I’ve opted to use a regular expression in my implementation, but your implementation may work with replacing substrings. Thus, there’s a bit commented out to give you the most flexibility.
This updates the
WHERE clause that’s fired to complete the work set in the initial part of the article where we add meta data.
There Are Other Ways
Note that there are some incredibly flexible ways to work with WP_Query to get the results that you need. And your implementation will vary depending on what it is that you’re doing.
So consider this one of many ways that can be used; however, know that if you have:
- a single key,
- a value (or a value that can be split into an array),
- and you want to search for metadata alongside the traditional post content
Then this will work.