这是一个有问题的GROUP BY
在以下情况下添加到WP\\U查询的子句tax_query
存在。它与s
参数您可以删除GROUP BY
完全是为了修复它,但您不应该对每个WP\\u查询都这样做。我经常使用以下课程来解决这个问题:
/**
* Search Within a Taxonomy
*
* Support search with tax_query args
*
* $query = new WP_Query( array(
* \'search_tax_query\' => true,
* \'s\' => $keywords,
* \'tax_query\' => array( array(
* \'taxonomy\' => \'country\',
* \'field\' => \'id\',
* \'terms\' => $country,
* ) ),
* ) );
*/
class WP_Query_Taxonomy_Search {
public function __construct() {
add_action( \'pre_get_posts\', array( $this, \'pre_get_posts\' ) );
}
public function pre_get_posts( $q ) {
if ( is_admin() ) return;
$wp_query_search_tax_query = filter_var(
$q->get( \'search_tax_query\' ),
FILTER_VALIDATE_BOOLEAN
);
// WP_Query has \'tax_query\', \'s\' and custom \'search_tax_query\' argument passed
if ( $wp_query_search_tax_query && $q->get( \'tax_query\' ) && $q->get( \'s\' ) ) {
add_filter( \'posts_groupby\', array( $this, \'posts_groupby\' ), 10, 1 );
}
}
public function posts_groupby( $groupby ) {
return \'\';
}
}
new WP_Query_Taxonomy_Search();
现在您可以添加自定义
search_tax_query
要将此功能添加到的WP\\u查询对象。例如:
$query = new WP_Query( array(
\'search_tax_query\' => true,
\'s\' => $keywords,
\'tax_query\' => array( array(
\'taxonomy\' => \'country\',
\'field\' => \'id\',
\'terms\' => $country,
) ),
) );
为了解释这里发生了什么,问题是由
GROUP BY
使用
tax_query
论点当您将此与搜索相结合时,我们不需要/不需要此子句。如果删除它,搜索结果将按预期工作:匹配搜索查询的帖子(
s
) 在分类法中(
tax_query
).