i want to search for multiple order numbers so i created this snippet:
function woo_multiple_order_search( $query_vars ) {
global $typenow;
global $wpdb;
global $pagenow;
if ( 'shop_order' === $typenow && isset( $_GET['s'] ) && 'edit.php' === $pagenow ) {
$search_term = esc_sql( sanitize_text_field( $_GET['s'] ) );
if (strpos($search_term, '|') == false)
return $query_vars;
$order_ids = explode('|',$search_term);
$meta_query = array(
'relation' => 'OR'
);
if(is_array($order_ids) && $order_ids) {
foreach($order_ids as $order_id) {
$meta_query[] = array(
'key' => '_order_number',
'value' => $order_id,
'compare' => '='
);
}
}
$args = array(
'posts_per_page' => -1,
'post_type' => 'shop_order',
'post_status' => 'any',
'meta_query' => $meta_query
);
$posts = get_posts( $args );
if ( ! $posts ) return $query_vars;
foreach($posts as $post){
$query_vars['post__in'][] = $post->ID;
}
}
return $query_vars;
}
add_filter( 'request', 'woo_multiple_order_search', 20 );
the logic behind is whenever “|” is detected inside the search string it should check inside a meta_key and return the results.
now i can verify that up to:
get_posts( $args );
it works as it gives the correct results.
However i can’t figure out how to return them to the admin list.
probably
$query_vars['post__in'][] = $post->ID;
is incorrect. any ideas?