WP_Query “OR”: Find posts by ID, Name or Post Title?

Hey there for some reason I have a function which has to find posts by either ID, name or post title. It looks like this:

/**
* @param $name_or_id: Name or ID of the post to find
* @param $posttype: The Post Type
* @return null|WP_Post
*/
function r2o_find_post($name_or_id,$posttype) {

if(is_numeric($name_or_id)) {
$id = abs(intval($name_or_id));
if($id > 0) {
$post = get_post($id);
if(is_object($post) && $post->post_type == $posttype) {
return $post;
}
}
return null;
}

$slargs = array(
‘post_title’ => $name_or_id,
‘post_type’ => $posttype,
‘post_status’ => ‘publish’,
‘numberposts’ => 1
);
$pt = get_posts($slargs);

if(is_array($pt)) {
return $pt[0];
}
return null;
}

However in $slargs I can only do:

post_title => $name_or_id
OR
post_name => $name_or_id

Can I do this “OR”-relationship with WP_Query/get_posts without doing two seperate queries?

Read more here:: WP_Query “OR”: Find posts by ID, Name or Post Title?

Leave a Reply

Your email address will not be published. Required fields are marked *