Using Wc_Get_Orders() Orderby Argument for a Custom Field in Woocommerce

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'
        ),
    )
);
5

1 Answer

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'
        ),
    )
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article
Twitter Facebook Pinterest