1

I am using this:

'orderby' => array(
    'meta_value_num' => 'ASC', 
    'menu_order' => 'ASC'
), 
'meta_query' => array(
    'relation' => 'AND',
    array(
        'key' => 'newprice',
        'type' => 'decimal',
    ),
    array(
        'key' => 'datum',
        'compare' => '>=',
        'value' => strtotime('-1days'),
    ),
),

Which works fine, except for the fact that this excludes any posts that don't contain a 'newprice' meta value.

1

In your meta query you also need explicitly query posts that don't have that meta key set. You can do this by nesting meta queries like so:

'orderby'    => array(
    'meta_value_num' => 'ASC', 
    'menu_order'     => 'ASC'
), 
'meta_query' => array(
    'relation' => 'AND',
    array(
        'relation' => 'OR',
        array(
            'key'  => 'newprice',
            'type' => 'decimal',
        ),
        array(
            'key'     => 'newprice',
            'value'   => '',
            'compare' => 'NOT EXISTS',
        ),
    ),
    array(
        'key'     => 'datum',
        'compare' => '>=',
        'value'   => strtotime('-1days'),
    ),
),

Also note that you need to set 'meta_key' on the query so that when you orderby 'meta_value_num' it orders by the right meta key.

Actually, it doesn't seem to be necessary if you have a meta_query, and excluding it will save you a join.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.