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

如何将WordPress搜索逻辑从AND关联改为OR关联?

Fixing WordPress/WooCommerce Search to Use OR Logic Instead of AND

Got it, let's tackle this frustrating search issue you're dealing with. It makes total sense that searching for "bunny" can't find your "bear and the bunny" product—WordPress and WooCommerce default to AND logic for search queries, meaning every term in your search has to exist in the content to show up. That's terrible for usability, so let's fix it with some custom code (since plugins aren't cutting it for you).

Step 1: Add Custom Code to Switch to OR Logic

You'll want to add this code to your theme's functions.php file, or better yet, a custom functionality plugin (this way it won't get wiped when you update your theme). Here's the code:

// Switch WordPress default search to use OR logic (works for posts, pages, and products)
add_filter('posts_search', 'wp_switch_search_to_or_logic', 10, 2);
function wp_switch_search_to_or_logic($search, $wp_query) {
    global $wpdb;

    // Skip if there's no search query or we're in the admin area
    if (empty($search) || is_admin()) {
        return $search;
    }

    // Only target frontend search queries
    if ($wp_query->is_search()) {
        // Replace all instances of AND with OR in the search SQL
        $search = str_replace('AND', 'OR', $search);
    }

    return $search;
}

// Extra handling for WooCommerce product searches (in case the above filter misses it)
add_action('woocommerce_product_query', 'wc_switch_product_search_to_or_logic');
function wc_switch_product_search_to_or_logic($query) {
    if (!is_admin() && $query->is_search() && $query->is_main_query()) {
        $search_clauses = $query->get('search_clauses');
        if (!empty($search_clauses)) {
            foreach ($search_clauses as &$clause) {
                $clause = str_replace('AND', 'OR', $clause);
            }
            $query->set('search_clauses', $search_clauses);
        }
    }
}

How This Works

  • The first filter (posts_search) hooks into WordPress's core search query and swaps out all AND operators with OR, so any search term match will return the content.
  • The second action (woocommerce_product_query) adds a safety net specifically for WooCommerce product searches, ensuring the OR logic applies there too.

Optional: Restrict to WooCommerce Only

If you only want to change the logic for product searches (and leave regular post/page search as AND), modify the first function's condition like this:

// Only target WooCommerce frontend searches
if ($wp_query->is_search() && is_woocommerce()) {

Testing It Out

After adding the code, try searching for "bunny" again—your "bear and the bunny" product should show up now. Searching for "bear" will still work, and searching for both "bear bunny" will return any product that has either term (or both).

If you have other search-related plugins active, you might need to adjust the filter priority (change 10 to a higher number like 99) to make sure this code runs after the plugin's logic.

内容的提问来源于stack exchange,提问作者Stan M

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:02:21