【问题标题】:Wordpress order by multiple meta keysWordpress 按多个元键排序
【发布时间】:2018-12-18 11:34:46
【问题描述】:

我正在为 Wordpress 查询的 orderby 选项而苦苦挣扎。 我想订购一个帖子列表,按是否存在键“其他值”的值排序。然后是项目的点击次数。

  1. 因此,首先是具有现有值的帖子,然后是不具有该值的帖子。
  2. 然后在现有和不存在的帖子中,用户点击的顺序是“按点击排序”键。

查询

'meta_query'    => array(
    'relation' => 'OR',
    array(
        'relation' => 'AND',
        array(
            'key' => 'sort-by-clicks'
        ),
        array(
            'key' => 'other-value',
            'compare' => 'EXISTS',
        )
    ),
    array(
        'relation' => 'AND',
        array(
            'key' => 'sort-by-clicks'
        ),
        array(
            'key' => 'other-value',
            'compare' => 'NOT EXISTS',
        )
    )
),

我尝试添加额外的参数来排序,但我不知道什么是正确的方法。

订单不成功 `

'othervalue' => array(
            'key' => 'other-value',
            'compare' => 'EXISTS',
        )

然后广告一个orderby

'orderby'  => array(
    'othervalue' => 'DESC'
)

我想这是正确的方法,但我看不到难题的尽头

【问题讨论】:

    标签: wordpress sql-order-by


    【解决方案1】:

    我已经找到了解决方案。请尝试使用以下代码:

    $args = array(
        'post_type'  => 'post_type_name',
        'meta_query' => array(
            'relation' => 'AND',
            'sortbyclicks' => array(
                'key'     => 'sort-by-clicks',
                'compare' => 'EXISTS',
            ),
            'othervalue' => array(
                'key'     => 'other-value',
                'compare' => 'EXISTS',
            ), 
        ),
        'orderby' => array(
            'othervalue' => 'DESC',        
        ),
    );
    
    $loop = new WP_Query( $args );

    谢谢!!

    【讨论】:

    • 感谢您的快速回复。但是您的解决方案仅显示存在其他值的帖子。我还想显示其他值不存在的帖子
    • 所以,你可以改变 wp 查询中的 'relation' 值,比如 'relation' => 'OR', ...试试看
    • 我认为这不是我正在寻找的解决方案。这是我需要的输出: - 值存在 20 次点击 - 值存在 3 次点击 - 值不存在 22 次点击 - 值不存在 5 次点击
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多