📝 WordPress: Fix Search Missing Posts Matched Only in Custom Fields
WordPress’s built-in search only matches against the post_title, post_content, and post_excerpt columns with a SQL LIKE comparison – it was never built to look inside postmeta at all. A theme or plugin that narrows search results with its own meta_query (limiting results to posts where a specific custom field is set, for instance) usually joins that condition onto the existing search with an implicit AND relation, which means a post only shows up if it satisfies BOTH the text search AND the meta condition. The moment a search term exists only inside a custom field’s value – a SKU, an internal reference code – and nowhere in the title, content, or excerpt, the text-search half of that AND fails on its own, and the otherwise perfectly matching post is dropped from the results entirely.
🔎 The Problem
add_action('pre_get_posts', function ($query) {
if ($query->is_search() && !is_admin()) {
$query->set('meta_query', [[
'key' => 'is_published_product',
'value' => '1',
]]);
// implicit relation with the search's own WHERE clause is AND:
// a post matching the meta_query but NOT the title/content/
// excerpt LIKE search (e.g. the term only appears in a SKU
// custom field) is silently excluded from results
}
});
✅ The Fix
- Hook posts_search (or posts_join plus posts_where) to add an explicit OR condition that also checks postmeta for a LIKE match on the relevant custom field, instead of relying on WP_Query’s default search columns to cover content that lives in meta.
- Keep any unrelated restriction (like ‘only published products’) as a genuine AND filter, but make sure it’s combined correctly so it narrows WHICH posts are eligible, without being chained in a way that also requires the search term itself to appear outside of meta.
- Test search specifically with a term that exists ONLY in a custom field value and nowhere else on the post, since a normal test using a term from the title will pass even while this exact bug silently drops meta-only matches.
🚨 Why This Is Dangerous
This produces a normal-looking results page, sometimes with other valid matches mixed in, so a missing item reads as ‘it isn’t in the catalog’ rather than ‘the search couldn’t see where the matching text actually lives’ – and nobody gets an error to point them at the real cause.
WordPress search was never built to read your custom fields – if your content lives there, your search filter has to go get it, not just ask search nicely to look.




