搜索时按后置元素排序

时间:2018-05-23 作者:Alex

我试图在搜索时按总销售额排序,我现在有这个,但似乎什么都没有做

add_action( \'pre_get_posts\', function ( $q )
{
    if (    !is_admin() // Target only front end queries
         && $q->is_main_query() // Target the main query only
         && ( $q->is_search() )
    ) {
        $q->set( \'meta_key\', \'total_sales\' );
        $q->set( \'order\',    \'DESC\'         );
        $q->set( \'orderby\',  \'meta_value_num\'  );
    }
});

1 个回复
SO网友:Alex

问题是我安装了一个处理搜索的插件,叫做更好的搜索。

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\' );
我所要做的就是抓住它。

结束