Using wc_get_orders() orderby argument for a custom field in WooCommerce

Viewed 3698

I have a meta field where there is a time entered e.g. 12:0,0 13:03, 01:00... I would like to orderby this field "billing_eta"

currently, I have the below code which I have added the orderby and order args to, however, they are ignored.

how can I order by the time in a meta field?

$args = array(
    'orderby' => 'billing_eta', //has no effect as its a meta field 
    'order' => 'DESC',
    'status'       => 'completed', // Accepts a string: one of 'pending', 'processing', 'on-hold', 'completed', 'refunded, 'failed', 'cancelled', or a custom order status.
    'meta_key'     => 'billing_date', // Postmeta key field
    'meta_value'   => $ppr_arrival_date, // Postmeta value field
    'meta_compare' => '==', // Possible values are ‘==’, ‘!=’, ‘>’, ‘>=’, ‘<‘, ‘<=’, ‘LIKE’, ‘NOT LIKE’, ‘IN’, ‘NOT IN’, ‘BETWEEN’, ‘NOT BETWEEN’, ‘EXISTS’ (only in WP >= 3.5), and ‘NOT EXISTS’ (also only in WP >= 3.5). Values ‘REGEXP’, ‘NOT REGEXP’ and ‘RLIKE’ were added in WordPress 3.7. Default value is ‘=’.
);

$orders = wc_get_orders( $args );

it is now work using the following if it helps anyone else:

$args = array(
    'status' => 'completed',
    'orderby' => 'eta',
    'order' => 'ASC',
    'meta_query' => array(
        'arrival_date' => array(
            'key' => 'billing_date',
            'value' => $ppr_arrival_date,
            'compare' => '='
        ),
        'eta' => array(
            'key' => 'billing_eta',
            'compare' => 'EXISTS',
            'type' => 'TIME'
        ),
    )
);
1 Answers

needed both meta elements in the meta_query thus:

$args = array(
    'status' => 'completed',
    'orderby' => 'eta',
    'order' => 'ASC',
    'meta_query' => array(
        'arrival_date' => array(
            'key' => 'billing_date',
            'value' => $ppr_arrival_date,
            'compare' => '='
        ),
        'eta' => array(
            'key' => 'billing_eta',
            'compare' => 'EXISTS',
            'type' => 'TIME'
        ),
    )
);
Related