我正在进行搜索。我可以按标签和类别搜索帖子,但不能按标题和内容搜索。
我尝试了下面的代码,但不起作用。你能帮帮我吗?
$getSearch = get_search_query();
//print($search);
$query = new WP_Query([
\'post_type\' => \'post\',
\'tax_query\' => array(
\'relation\' => \'OR\',
array(
\'s\'=> $getSearch // for title and content
),
array(
\'taxonomy\' => \'post_tag\', //for tag
\'field\' => \'slug\',
\'terms\' => array($getSearch),
),
array(
\'taxonomy\' => \'category\', // for category
\'field\' => \'slug\',
\'terms\' => array($getSearch),
),
),
]);
if ($query->have_posts() ):
while ( $query->have_posts() ) {$query->the_post();?>
<!--some content-->
<?php }
else :
get_template_part( \'template-parts/content\', \'none\' );
endif;
?>
?>function.php
add_action( \'pre_get_posts\', \'my_search_exclude\' );
function my_search_exclude( $query ) {
if ( ! $query->is_admin && $query->is_search && $query->is_main_query() ) {
$query->set( \'post_type\', array( \'post\', \'page\', \'product\' ) );
$query->set( \'post__not_in\', array( 471 ) );
$taxquery= array(
\'post_type\' => \'any\',
\'s\'=> $query->get(\'s\')
);
$query->set( \'tax_query\', $taxquery);
}
}