如何创建一个WP_QUERY来搜索标题或标签?

时间:2019-01-15 作者:Matthew Woodard

我使用WP\\u Query创建了一个搜索,这个查询似乎是在标题和标签中查找查询到的术语。

有没有办法让这个搜索找到标题或标签?

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

$paged = $request[\'page\'];
$posts_per_page = $request[\'per_page\'];

$result = new WP_Query([
    \'post_type\'         => \'post\',
    \'category__in\'      => 3060,
    \'posts_per_page\'    => $posts_per_page,
    \'paged\'             => $page,
    \'orderby\'           => \'date\',
    \'order\'             => \'desc\',
    \'s\'                 => $s,
    \'tag\'               => array($tags)
]);

1 个回复
SO网友:Er Deepak Prabhakar

请使用下面的代码以文本和标题显示帖子

$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;