custom post types - Stuck in Order by more then one

admin2025-04-20  1

I want to orderby post by 2 custom fields and alphabetical here is my code

'meta_query'    => array(
    array(
        'relation' => 'AND',
        'awards' => array(
            'key'       => 'awards',
            'compare'   => 'EXISTS',
        ),
        'points' => array(
            'key'       => 'points',
            'compare'   => 'EXISTS',
        ),
    ),
),
'orderby' => array(

    'awards'       => 'desc',
    'points'     => 'desc',
),

So if the awards are same so it will order by points but if the points will same also so it will orber by alphabetical.

I want to orderby post by 2 custom fields and alphabetical here is my code

'meta_query'    => array(
    array(
        'relation' => 'AND',
        'awards' => array(
            'key'       => 'awards',
            'compare'   => 'EXISTS',
        ),
        'points' => array(
            'key'       => 'points',
            'compare'   => 'EXISTS',
        ),
    ),
),
'orderby' => array(

    'awards'       => 'desc',
    'points'     => 'desc',
),

So if the awards are same so it will order by points but if the points will same also so it will orber by alphabetical.

Share Improve this question asked Sep 28, 2019 at 18:32 HammadHammad 413 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You've got the unnecessary array involved around clauses. The correct code is

'meta_query' => array(
    'relation' => 'AND',
    'awards_clause'   => array( // clause
        'key'     => 'awards',
        'compare' => 'EXISTS',
    ),
    'points_clause'   => array( // clause
        'key'     => 'points',
        'compare' => 'EXISTS',
    ),
),
'orderby'    => array(
    'awards_clause' => 'DESC',
    'points_clause' => 'DESC',
),
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745121005a286145.html

最新回复(0)