Pre_Get_Posts操作中的多个orderby

时间:2014-12-01 作者:pixeline

我过去能够按2个条件对查询结果进行排序(“先按状态=未售出的ASC,然后按日期描述排序结果”),如下所示:

add_action( \'pre_get_posts\', \'my_get_posts\' );

function my_get_posts( $query )
{
    if (is_admin()){
        return;
    }

   if (is_post_type_archive(\'objet\')){

            // Stock: sort by unsold first, then by date
            $query->set(\'meta_key\', \'wpcf-object-sold-status\' );
            $query->set(\'orderby\', \'meta_value date\');
            $query->set(\'order\', \'ASC DESC\' );
        }
    return $query;
}
但现在,它不再改变结果顺序。我不知道它为什么停止工作。可能是Wordpress 3到4的更新?

1 个回复
最合适的回答,由SO网友:skim- 整理而成

正如米洛所说:

$query->set(\'meta_key\', \'wpcf-object-sold-status\' );
$query->set(\'orderby\', array(\'meta_value\' => \'ASC\', \'date\' => \'DESC\'));
// $query->set(\'order\', \'ASC DESC\' ); // not needed
相关链接:https://make.wordpress.org/core/2014/08/29/a-more-powerful-order-by-in-wordpress-4-0/

结束

相关推荐

Can't use orderby in WP_Query

我无法在日期前订购我的帖子,descading。我有以下代码:$jobs = new WP_Query(array( \'posts_per_page\' => 50, \'post_type\' => \'job\', \'post_status\' => \'publish\', \'orderby\' => \'post_date\', \'order\' => \'DESC\',