You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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_order and shop_subscription post types, so the custom field search works for both.
  • Preserves default search: Instead of clearing the s parameter, 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:

  1. Replace your existing code with this snippet (add it to your child theme's functions.php file or a custom plugin).
  2. Adjust the $custom_fields array to include all the custom meta fields you want to search.
  3. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:11:38