I'm sure someone else had this need somewhere... I just couldn't find it!
So I have a search field where I want to write something and what I write returns me WooCommerce orders.
This is my working code that searchs for first and last billing names:
$args = array(
'post_type' => 'shop_order',
'posts_per_page' => '-1',
'post_status' => array_keys( wc_get_order_statuses() ),
'meta_query' => array(
'relation' => 'OR',
array(
'key' => '_billing_first_name',
'compare' => 'LIKE',
'value' => $_GET['sc'],
),
array(
'key' => '_billing_last_name',
'compare' => 'LIKE',
'value' => $_GET['sc'],
)
)
);
But I also want to be able to search for post ID so that I can allow, on that one field, for users to search for order ID, first and last billing names.
How can I achieve this?
EDIT:
After the suggestion from the user mukto90, I did some changes and the code now works perfectly.
$args = array(
'post_type' => 'shop_order',
'posts_per_page' => '-1',
'post_status' => array_keys( wc_get_order_statuses() ),
);
if( 'shop_order' == get_post_type( $sc_esc ) ) {
$args['p'] = $sc_esc;
} else {
$args['meta_query'] = array(
'relation' => 'OR',
array(
'key' => '_billing_first_name',
'compare' => 'LIKE',
'value' => $sc_esc,
),
array(
'key' => '_billing_last_name',
'compare' => 'LIKE',
'value' => $sc_esc,
)
);
}
I'm sure someone else had this need somewhere... I just couldn't find it!
So I have a search field where I want to write something and what I write returns me WooCommerce orders.
This is my working code that searchs for first and last billing names:
$args = array(
'post_type' => 'shop_order',
'posts_per_page' => '-1',
'post_status' => array_keys( wc_get_order_statuses() ),
'meta_query' => array(
'relation' => 'OR',
array(
'key' => '_billing_first_name',
'compare' => 'LIKE',
'value' => $_GET['sc'],
),
array(
'key' => '_billing_last_name',
'compare' => 'LIKE',
'value' => $_GET['sc'],
)
)
);
But I also want to be able to search for post ID so that I can allow, on that one field, for users to search for order ID, first and last billing names.
How can I achieve this?
EDIT:
After the suggestion from the user mukto90, I did some changes and the code now works perfectly.
$args = array(
'post_type' => 'shop_order',
'posts_per_page' => '-1',
'post_status' => array_keys( wc_get_order_statuses() ),
);
if( 'shop_order' == get_post_type( $sc_esc ) ) {
$args['p'] = $sc_esc;
} else {
$args['meta_query'] = array(
'relation' => 'OR',
array(
'key' => '_billing_first_name',
'compare' => 'LIKE',
'value' => $sc_esc,
),
array(
'key' => '_billing_last_name',
'compare' => 'LIKE',
'value' => $sc_esc,
)
);
}
Share
Improve this question
edited Mar 10, 2022 at 19:58
Pedro
asked Mar 9, 2022 at 2:42
PedroPedro
338 bronze badges
1
|
1 Answer
Reset to default 1Use this code instead-
$args = array(
'post_type' => 'shop_order',
'posts_per_page' => '-1',
'post_status' => array_keys( wc_get_order_statuses() ),
'meta_query' => array(
'relation' => 'OR',
array(
'key' => '_billing_first_name',
'compare' => 'LIKE',
'value' => $_GET['sc'], // you should escape this
),
array(
'key' => '_billing_last_name',
'compare' => 'LIKE',
'value' => $_GET['sc'], // you should escape this
)
)
);
// if there is an order that has the same ID as what user inputs, include that in the query too
if( 'shop_order' == get_post_type( $_GET['sc'] ) ) {
$args['p'] = $_GET['sc']; // you should escape this
}
123
, and you have an order withID
=123
also another order with_billing_last_name
=123
? (I know that's very unlikely, but you need to be sure) – mukto90 Commented Mar 9, 2022 at 2:52