You could do something like this
add_action('pre_get_posts', 'bt_search_posts');
function bt_search_posts ($query) {
// the posts ids that contain foo bar OR lorem ipsum dolor OR ping pong
$posts_ids = [];
// get the current query vars
$query_vars = $query->query_vars;
// loop each term we want to search and if found, add the post id into the array
foreach (['foo bar', 'lorem ipsum dolor', 'ping pong'] as $search_term) {
// add the search term to the query vars
$query_vars['s'] = $search_term;
// we only need posts ids so we set that
$query_vars['fields'] = 'ids';
// loop through found posts, if not found we will have a empty array so no problem
foreach (get_posts($query_vars) as $post_id) {
$posts_ids[] = $post_id;
}
}
// set post__in to the posts ids that we found that matched the search terms we provided
if (!empty($posts_ids)) $query->set('post__in', $posts_ids);
}
I have no idea where you want to run this query so you need to add the proper checks so you won’t interfere with other WP_Query
, like the main WordPress query and the like.
A good starting check would be
if (!is_admin() && $query->is_main_query()) {
// code here
}
But if you already have one that works best for you, use it =]