Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kingjmaningo/cd970aa4c03b927f36d5882314cab893 to your computer and use it in GitHub Desktop.
Save kingjmaningo/cd970aa4c03b927f36d5882314cab893 to your computer and use it in GitHub Desktop.
Recognise order meta for you to b e able to use it in your args to display specific orders - Woocommerce
<?php
// add this filter to recognise meta key when you do your order query
add_filter( 'woocommerce_order_data_store_cpt_get_orders_query', 'my_function', 10, 2 );
function my_function( $query, $query_vars ) {
if ( ! empty( $query_vars['_my_custom_order_meta'] ) ) {
$query['meta_query'][] = array(
'key' => '_my_custom_order_meta',
'value' => esc_attr( $query_vars['_my_custom_order_meta'] ),
);
}
return $query;
}
// Then when you want to get orders under that meta key
$args = array(
'_my_custom_order_meta' => 'your_meta_value',
);
$new_order_query = new WC_Order_Query( $args );
$orders = $new_order_query->get_orders();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment