How to create a WP_Query to search the Title or Tag?

Please use below code to show posts in texonomy and titles

$s = $request['s'];
$tags = str_replace(' ', '-', strtolower($request['s']));

$q1 = get_posts(array(
        'fields' => 'id',
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        's' =>  $s 

));
 $q2 = get_posts(array(
        'fields' => 'ids',
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'tag' => array($tags)
));
$unique = array_unique( array_merge( $q1, $q2 ) );

$posts = get_posts(array(
    'post_type' => 'post',
    'post__in' => $unique,
    'posts_per_page' => -1
));
  if ($posts ) : 

foreach( $posts as $post ) :
//show results
endforeach;
endif;

Leave a Comment