WC_Order_Data_Store_CPT::search_orders( string $term )
Search order data for a term and return ids.
Description Description
Parameters Parameters
- $term
-
(Required) Searched term.
Return Return
(array) of ids
Source Source
File: includes/data-stores/class-wc-order-data-store-cpt.php
public function search_orders( $term ) {
global $wpdb;
/**
* Searches on meta data can be slow - this lets you choose what fields to search.
* 3.0.0 added _billing_address and _shipping_address meta which contains all address data to make this faster.
* This however won't work on older orders unless updated, so search a few others (expand this using the filter if needed).
*
* @var array
*/
$search_fields = array_map(
'wc_clean',
apply_filters(
'woocommerce_shop_order_search_fields',
array(
'_billing_address_index',
'_shipping_address_index',
'_billing_last_name',
'_billing_email',
)
)
);
$order_ids = array();
if ( is_numeric( $term ) ) {
$order_ids[] = absint( $term );
}
if ( ! empty( $search_fields ) ) {
$order_ids = array_unique(
array_merge(
$order_ids,
$wpdb->get_col(
$wpdb->prepare(
"SELECT DISTINCT p1.post_id FROM {$wpdb->postmeta} p1 WHERE p1.meta_value LIKE %s AND p1.meta_key IN ('" . implode( "','", array_map( 'esc_sql', $search_fields ) ) . "')", // @codingStandardsIgnoreLine
'%' . $wpdb->esc_like( wc_clean( $term ) ) . '%'
)
),
$wpdb->get_col(
$wpdb->prepare(
"SELECT order_id
FROM {$wpdb->prefix}woocommerce_order_items as order_items
WHERE order_item_name LIKE %s",
'%' . $wpdb->esc_like( wc_clean( $term ) ) . '%'
)
)
)
);
}
return apply_filters( 'woocommerce_shop_order_search_results', $order_ids, $term, $search_fields );
}