user3514052 user3514052 - 5 days ago 6
PHP Question

wordpress order query by two meta keys

I want to order posts by two meta keys "post_up_lvl" (dominant) and "post_up_exp".

For ex. output must be 44321111. If post_up_lvl repeat must order by post_up_exp where the date is stored. I tried that, but it arranges only by "post_up_lvl".

$qargs1 = array(

'post_type' => 'post',
'posts_per_page' => -1,
'paged' => $paged,
'meta_query' => array(
'relation' => 'AND',
'levels' => array(
'key' => 'post_up_lvl'
),
'dates' => array(
array(
'key' => 'post_up_exp',
'value' => $today,
'compare' => '>=',
'type' => 'datetime'
)
)
),
'orderby' => array(
'levels' => 'DSC',
'dates' => 'DSC'
),

);

Answer

The parameter dates doesn't exist, you must use date_query instead, I never heard about levels parameter in WP_Query,

Here is an idea for the meta_query and date_query, the array must remains, you can add/remove/modify arguments as your needs with the help of the link above,

    'meta_query' => array(
         'relation' => 'AND'
            array(
                'key' => 'post_up_lvl',
                'value' => $today,
                'type' => 'DATETIME'
            )
            array(
                'key' => 'post_up_exp',
                'value' => $today,
                'type' => 'DATETIME'
            )
    ),
    'date_query' => array(
        array(
            'key' => 'post_up_exp',
            'value' => $today,
            'compare' => '>=',
            'type' => 'datetime'
        )
    )
Comments