How to query_posts using meta_query to orderby meta_key AND have a secondary sort by date?

I've been troubleshooting an issue over the past few days and keep going in circles. Could really use a fresh pair of eyes to help me answer this question...

So I'm working with a wordpress site where posts have a few custom fields relevant to the query I need to run: "post-expired" and "my-sort-order". When I run my query_posts I want the results to be posts where the "post-expired" is NOT "yes" (this part works in my code). I also want the results sorted by the DESC number value of "my-sort-order" (works as well) AND if there's a tie on "my-sort-order" I would like a secondary sort by date with the most recent posts first.

The secondary sort by date is where I'm having trouble.

Here is the code I have right now:

<?php               
$args = array(
    'paged' => $paged,
    'meta_query'=> array(
        array(
            'key'=>'post-expired',
            'value'=> 'yes',
            'compare' => '!='
        )
    ),
    'meta_key'=>'my-sort-order',
    'orderby'=>'meta_value_num', 
    'order'=>'DESC'
);

$args = array_merge( $args , $wp_query->query );

query_posts( $args );
?>


Using the code above I correctly get back the non-expired posts and they're sorted by my-sort-order descending (i.e. 100 shows up before 99, etc). But if both posts have the same sort order value there is some type of secondary sort happening I can't seem to control (and can't figure out what it is actually being sorted on).

My first idea to solve this was to only add values to "my-sort-order" if I wanted to set a specific sort order for that post. I thought if the value for this field was left blank for the rest of the posts, they would simply be returned in the default sort by DESC date (after the posts with a set sort order). However, what actually happened was any posts that didn't have a sort order value set were NOT returned at all...

Next, I tried adding multiple values in the orderby field like this:

('orderby'=>'meta_value_num date')


That completely blew up both sort orders I was going for and returned the posts in an unexpected way. I was under the impression that multiple orderby values were allowed, but for some reason it's not working here.

At this point I'm not sure how to make either of these potential solutions work. Does anyone know how I can either 1) Sort posts by the "my-sort-order" field first, then have the remaining posts with no value in that field returned with the default date sort; or 2) Figure out how to control the secondary orderby sort so any posts that have a tie on "my-sort-order" will be sorted by date (newest first)?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值