function ajax_search() { if ( ! apply_filters( 'woosc_disable_nonce_check', false, 'search' ) ) { if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['nonce'] ), 'woosc-security' ) ) { die( 'Permissions check failed!' ); } } $keyword = sanitize_text_field( $_POST['keyword'] ?? '' ); $products = self::get_products(); global $wpdb; $args = [ 'post_type' => 'product', 'post_status' => 'publish', 'posts_per_page' => self::get_setting( 'search_count', 10 ), 'fields' => 'ids', 'ignore_sticky_posts' => 1, 'orderby' => 'menu_order title', 'order' => 'ASC', ]; // Exclude already compared products if ( ! empty( $products ) ) { $args['post__not_in'] = $products; } // Add filters for WHERE, JOIN, GROUPBY add_filter( 'posts_where', function ( $where ) use ( $keyword ) { global $wpdb; if ( empty( $keyword ) ) return $where; $like = '%' . $wpdb->esc_like( $keyword ) . '%'; $where .= $wpdb->prepare(" AND ( {$wpdb->posts}.post_title LIKE %s OR woosc_sku_meta.meta_value LIKE %s OR {$wpdb->posts}.ID IN ( SELECT tr.object_id FROM {$wpdb->term_relationships} tr INNER JOIN {$wpdb->term_taxonomy} tt ON tr.term_taxonomy_id = tt.term_taxonomy_id INNER JOIN {$wpdb->terms} t ON t.term_id = tt.term_id WHERE tt.taxonomy IN ('product_cat','product_tag') AND t.name LIKE %s ) ) ", $like, $like, $like ); return $where; }); add_filter( 'posts_join', function ( $join ) { global $wpdb; if ( defined( 'DOING_AJAX' ) && DOING_AJAX && isset( $_POST['keyword'] ) ) { $join .= " LEFT JOIN {$wpdb->postmeta} AS woosc_sku_meta ON ({$wpdb->posts}.ID = woosc_sku_meta.post_id AND woosc_sku_meta.meta_key = '_sku')"; } return $join; }); add_filter( 'posts_groupby', function ( $groupby ) { global $wpdb; return "{$wpdb->posts}.ID"; }); // Run search query $query = new WP_Query( $args ); // Output results if ( $query->have_posts() ) { echo ''; } else { echo ''; } wp_die(); }