Skip to content Skip to sidebar

This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 -

Supporter timezone: Asia/Singapore (GMT+08:00)

This topic contains 30 replies, has 0 voices.

Last updated by Kor 1 week ago.

Assisted by: Kor.

Author Posts
August 21, 2025 at 1:58 am #17335707

Kor
WPML Supporter since 08/2022

Languages: English (English )

Timezone: Asia/Singapore (GMT+08:00)

Thanks for your patience. Our 2nd Tier Support checked, and we've modified this PHP file.

/wp-content/plugins/vm-to-wc-migration/vm-to-wc-migration.php

Replacing

add_filter('posts_where', 'extend_product_search_sku_where');
function extend_product_search_sku_where($where) {
	global $wpdb;

	if (is_search() && !is_admin()) {
		$search = trim(get_query_var('s'));
		$search = esc_sql($search);
		$where .= " OR ({$wpdb->postmeta}.meta_key = '_sku' AND {$wpdb->postmeta}.meta_value LIKE '%{$search}%')";
	}

//	error_log('--------- where -------------');
//	error_log($where);

	return $where;
}

With

add_filter('posts_search', function ($search_sql, WP_Query $q) {
    if ( is_admin() || !$q->is_search() ) {
        return $search_sql;
    }

    // Only apply when actually searching and to product queries
    $s = $q->get('s');
    if ($s === '' || $s === null) {
        return $search_sql;
    }

    // If caller didn't restrict post_type, keep results sane
    $pt = $q->get('post_type');
    if (empty($pt) || $pt === 'any') {
        $q->set('post_type', ['product', 'product_variation']);
    }

    global $wpdb;
    $like = '%' . $wpdb->esc_like($s) . '%';

    // Append an OR ... EXISTS(...) *inside* the search parentheses
    $skuExists = $wpdb->prepare("
        OR EXISTS (
            SELECT 1
            FROM {$wpdb->postmeta} pm
            WHERE pm.post_id = {$wpdb->posts}.ID
              AND pm.meta_key = '_sku'
              AND pm.meta_value LIKE %s
        )
    ", $like);

    // Heuristic: the core search SQL ends with a closing ')'
    // We tack our OR ... before that closing ')'
    // This keeps WPML's language filters (added as separate ANDs) intact.
    $search_sql = preg_replace('/\)\s*$/', $skuExists . ')', $search_sql, 1);

    return $search_sql;
}, 20, 2);

Could you please check and let me know if the issue is resolved?