The default search returns all post types, which is what I want. But for one specific custom post type I want to limit the result to a specific meta key value (a custom date that must be greater than today).
Using the pre_get_posts hook and setting the meta_query does get me the correct result for that post type, but it also filters out all post types that do not have that key set.
Is it possible to use the meta_query only for one specific cpt?
That should be clear enough but here's some code nonetheless:
function searchExcludeOldMatches($query) {
if (!is_admin()&&$query->is_search) {
$query->set('meta_query', array(
array(
'key' => 'cpt-match-date'
,'value' => date('Y/m/d',time())
,'type' => 'DATE'
,'compare' => '>'
)
));
};
return $query;
};
add_filter('pre_get_posts','searchExcludeOldMatches');