No. WP_Query
class doesn’t support OR
based conditional query on same table’s columns. So in this case you have to write your own SQL query. And as there is a query must to get this single post, it’s not gonna hamper the performance. 🙂
However, I have re-write your code a slightly different way. This way you wouldn’t need to use both get_post()
and get_posts()
. Only one get_posts()
call would be enough. Here is 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 ) {
$slargs = array(
'post_title' => $name_or_id,
'post_type' => $posttype,
'post_status' => 'publish',
'posts_per_page' => 1,
);
if( is_numeric( $name_or_id ) ) {
$id = abs( intval( $name_or_id ) );
if( $id > 0 ) {
unset( $slargs[ 'post_title' ] );
$slargs['post__in'] = [ $name_or_id ];
}
}
$pt = get_posts( $slargs );
if( is_array($pt) ) {
return $pt[0];
}
return null;
}
My main theory above there is, we are manipulating the arguments of get_posts()
based on the type of name_or_id
parameter.
Hope this above helps.