/**
* Get products based on search query with merchant filtering
*/
private function get_search_products($atts) {
global $wpdb;
$search_term = $atts['search'];
$search_mode = isset($atts['search_mode']) ? $atts['search_mode'] : 'comprehensive';
$product_ids = array();
// Mode 1: Basic WordPress search (fastest but limited)
if ($search_mode === 'basic') {
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
's' => $search_term,
'posts_per_page' => -1,
'fields' => 'ids'
);
$query = new WP_Query($args);
$product_ids = $query->posts;
}
// Mode 2: Title only search using LIKE
elseif ($search_mode === 'title_only') {
$product_ids = $wpdb->get_col($wpdb->prepare("
SELECT ID
FROM {$wpdb->posts}
WHERE post_type = 'product'
AND post_status = 'publish'
AND post_title LIKE %s
", '%' . $wpdb->esc_like($search_term) . '%'));
}
// Mode 3: Comprehensive search (default)
else {
// Direct SQL search for maximum compatibility
$search_like = '%' . $wpdb->esc_like($search_term) . '%';
// Search in titles and content
$title_content_ids = $wpdb->get_col($wpdb->prepare("
SELECT ID
FROM {$wpdb->posts}
WHERE post_type = 'product'
AND post_status = 'publish'
AND (post_title LIKE %s OR post_content LIKE %s OR post_excerpt LIKE %s)
", $search_like, $search_like, $search_like));
if (!empty($title_content_ids)) {
$product_ids = array_merge($product_ids, $title_content_ids);
}
// Search by SKU
$sku_ids = $wpdb->get_col($wpdb->prepare("
SELECT post_id
FROM {$wpdb->postmeta}
WHERE meta_key='_sku'
AND meta_value LIKE %s
", $search_like));
if (!empty($sku_ids)) {
$product_ids = array_merge($product_ids, $sku_ids);
}
// Search in product categories
$cat_ids = $wpdb->get_col($wpdb->prepare("
SELECT tr.object_id
FROM {$wpdb->term_relationships} tr
JOIN {$wpdb->term_taxonomy} tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
JOIN {$wpdb->terms} t ON tt.term_id = t.term_id
WHERE tt.taxonomy = 'product_cat'
AND t.name LIKE %s
", $search_like));
if (!empty($cat_ids)) {
$product_ids = array_merge($product_ids, $cat_ids);
}
}
// Remove duplicates
$product_ids = array_unique($product_ids);
if (empty($product_ids)) {
return array();
}
// Filter out hidden products
$visible_product_ids = array();
foreach ($product_ids as $product_id) {
$product = wc_get_product($product_id);
if ($product && $product->is_visible()) {
$visible_product
Caps Archives - Page 29 of 38 - Claire's Picks
-
$125.00 Original price was: $125.00.$106.00Current price is: $106.00.
-
$18.29 Original price was: $18.29.$7.69Current price is: $7.69.
-
$39.99
-
$29.99
-
$40.00
-
$84.00
-
$170.41 Original price was: $170.41.$102.25Current price is: $102.25.
-
$168.23 Original price was: $168.23.$100.94Current price is: $100.94.
-
$50.00 Original price was: $50.00.$25.00Current price is: $25.00.
-
$50.00 Original price was: $50.00.$25.00Current price is: $25.00.
-
$41.00 Original price was: $41.00.$28.00Current price is: $28.00.
-
$45.00 Original price was: $45.00.$22.00Current price is: $22.00.
Go to top
Shopping Cart 0
No products in the basket.