我正在尝试通过postmeta total_sales
function ta_modify_main_query($query) {
if ($query->is_search()) {
$query->set('orderby', 'meta_value_num');
$query->set('meta_key', 'total_sales');
$query->set('order', 'DESC');
}
}
add_action( 'pre_get_posts', 'ta_modify_main_query', 999 );
我认为代码很好,但它不起作用,查询时不会更改查询。我的网址看起来像?s=abstract+pattern&post_type=product
,使用的查询是
SELECT SQL_CALC_FOUND_ROWS wp_posts.*, ID, (MATCH(post_title) AGAINST ('abstract pattern' ) * 1 ) + (MATCH(post_content) AGAINST ('abstract pattern' ) * 10 ) AS score
FROM wp_posts
INNER JOIN wp_postmeta
ON ( wp_posts.ID = wp_postmeta.post_id )
WHERE 1=1
AND MATCH (post_title,post_content) AGAINST ('abstract pattern' )
AND (post_status = 'publish'
OR post_status = 'inherit')
AND wp_posts.post_type IN ('product')
GROUP BY wp_posts.ID
ORDER BY score DESC
LIMIT 0, 12
答案 0 :(得分:0)
问题是我安装了一个处理搜索的插件,称为更好搜索。
function query_1($query){
$query .= ",wp_postmeta.meta_value as total_saless ";
return $query;
}
add_filter( 'bsearch_posts_match_field', 'query_1' );
function query_2($query){
$query .= "AND wp_postmeta.meta_key='total_sales' ";
return $query;
}
add_filter( 'bsearch_posts_where', 'query_2' );
function query_3($query){
$query = " CAST(total_saless AS INTEGER) > 99999 DESC, score DESC ";
return $query;
}
add_filter( 'bsearch_posts_orderby', 'query_3' );
我所要做的只是勾住它。