Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1.2K
Views
Extend wc_get_orders() with a custom meta key and meta value

I'm trying to get all the shop orders with a meta key "order_referrer_id" and the value will be a user id for example "1060".

This is what i have tried so far:

$args = array(
        'limit' => -1,
        'status' => 'completed',
        'meta_query' => array(
            array(
                'key' => 'order_referrer_id',
                'value' => 1060,
                'compare' => '='
            ),
        ),
        'return' => 'ids',
    ); 

$orders = wc_get_orders( $args );

For some reason the query ignore the meta query and returns all the orders.

What is the correct way to filter shop orders?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

wc_get_orders and WC_Order_Query provide a standard way of retrieving orders that is safe to use and will not break due to database changes in future WooCommerce versions.

Source: wc_get_orders and WC_Order_Query


You can use meta_key, meta_value and meta_compare, so you get:

$args = array(
    'status'        => 'completed', // Accepts a string: one of 'pending', 'processing', 'on-hold', 'completed', 'refunded, 'failed', 'cancelled', or a custom order status.
    'meta_key'      => 'order_referrer_id', // Postmeta key field
    'meta_value'    => 1060, // 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 ‘=’.
    'return'        => 'ids' // Accepts a string: 'ids' or 'objects'. Default: 'objects'.
);

$orders = wc_get_orders( $args );

// NOT empty
if ( ! empty ( $orders ) ) {  
    foreach ( $orders as $order ) {
        echo '<p>ID = ' . $order . '</p>';
    }
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!