WooCommerce扩展搜索功能:支持自定义字段搜索订单与订阅
Fix: WooCommerce Search for Custom Fields on Both Orders and Subscriptions
Got it, let's get your search working for both orders and subscriptions while keeping the default title/content search functionality intact. The issue with your original code is twofold: it only targets standard order queries, and it replaces the default search instead of supplementing it with custom field checks.
Here's the revised code that addresses both problems:
function wc_search_include_custom_fields_orders_subscriptions( $query ) { // Only modify admin search queries for WooCommerce orders and subscriptions if ( ! is_admin() || ! $query->is_search || ! in_array( $query->get( 'post_type' ), array( 'shop_order', 'shop_subscription' ), true ) ) { return; } $custom_fields = array( 'gender', 'birthdate' ); // Add your custom fields here $search_term = $query->query_vars['s']; if ( empty( $search_term ) ) { return; } // Grab any existing meta query to avoid overwriting it $meta_query = $query->get( 'meta_query', array() ); // Build our custom field search conditions (OR logic between fields) $custom_field_conditions = array( 'relation' => 'OR' ); foreach ( $custom_fields as $field ) { $custom_field_conditions[] = array( 'key' => $field, 'value' => $search_term, 'compare' => '=' ); } // Merge our conditions with existing meta query (OR logic with default search) if ( ! empty( $meta_query ) ) { $meta_query = array( 'relation' => 'OR', $meta_query, $custom_field_conditions ); } else { $meta_query = $custom_field_conditions; } // Update the query with our combined meta conditions $query->set( 'meta_query', $meta_query ); // We leave the 's' parameter intact so default title/content search still works! } add_filter( 'pre_get_posts', 'wc_search_include_custom_fields_orders_subscriptions' );
Key improvements over your original code:
- Targets both orders and subscriptions: We explicitly check for the
shop_orderandshop_subscriptionpost types, so the custom field search works for both. - Preserves default search: Instead of clearing the
sparameter, we keep it active. This means users can still search by order/subscription title or content, and also find matches in your custom fields. - Safe merging with existing queries: We handle any pre-existing meta query conditions to avoid breaking other WooCommerce functionality.
- Admin-only restriction: The code only runs in the WordPress admin area, where you'd typically search orders and subscriptions.
How to use:
- Replace your existing code with this snippet (add it to your child theme's
functions.phpfile or a custom plugin). - Adjust the
$custom_fieldsarray to include all the custom meta fields you want to search. - Test it out: Go to your WooCommerce Orders or Subscriptions page, enter a value from one of your custom fields in the search bar—you should see matching orders/subscriptions show up alongside any results from the default title/content search.
内容的提问来源于stack exchange,提问作者Nathan Winch
相关产品推荐
相关产品推荐

