Error finding specific post_type column of post_type form using WooCommerce product search

I use this code to search for products from Wordpress / WooCommerce .
My requirement is that the URL will look like http: // localhost / wp /? S = D34 & post_type = product "While s=D34 is the search string.

When the user will search for a string. Data will be searched in all fields by default + product custom filed . Below code works fine with http: // localhost / wp /? S = D34 , but when &post_type=product combined with the url, then it says enter image description here

The code is below

 function cf_search_where( $where ) { global $pagenow, $wpdb; if ( is_search() ) { $where = preg_replace("/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/", "(".$wpdb->posts.".post_title LIKE $1) OR (".$wpdb->postmeta.".meta_value LIKE $1)", $where ); $where .= " AND ($wpdb->posts.post_type = 'product') "; } return $where; } add_filter( 'posts_where', 'cf_search_where' ); 

This is necessary to prevent various values.

  function cf_search_distinct( $where ) { global $wpdb; if ( is_search() ) { return "DISTINCT"; //to prevent duplicates } return $where; } add_filter( 'posts_distinct', 'cf_search_distinct' ); 

So what kind of modification is required?

URL http: // localhost / wp /? Orderby = price & post_type = product works fine

but what is wrong with http: // localhost / wp /? s = D34 & post_type = product

+7
post php search wordpress woocommerce
source share
1 answer

try it

 function cf_search_where( $where ) { global $pagenow, $wpdb; // a little debugging will help you.. //print_r ($where); //die(); if ( is_search() ) { $where = preg_replace("/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/", "(".$wpdb->posts.".post_title LIKE $1) OR (".$wpdb->postmeta.".meta_value LIKE $1)", $where ); $where .= " AND ($wpdb->posts.post_type = 'product') "; } return $where; } add_filter( 'posts_where', 'cf_search_where' ); 

Based on your updated question.

if only you used print_r ($where); to check what the $where value contains, you will see something like this ...

with http: // localhost / wp /? s = D34

 AND (((wp1_posts.post_title LIKE '%D34%') OR (wp1_postmeta.meta_value LIKE '%D34%') OR (wp1_posts.post_content LIKE '%D34%'))) AND (wp1_posts.post_password = '') AND wp1_posts.post_type IN ('post', 'page', 'attachment', 'product') AND (wp1_posts.post_status = 'publish') 

with http: // localhost / wp /? s = D34 & post_type = product

 AND (((wp1_posts.post_title LIKE '%D34%') OR (wp1_postmeta.meta_value LIKE '%D34%') OR (wp1_posts.post_content LIKE '%D34%'))) AND (wp1_posts.post_password = '') AND ( ( wp1_postmeta.meta_key = '_visibility' AND CAST(wp1_postmeta.meta_value AS CHAR) IN ('visible','search') ) ) AND wp1_posts.post_type = 'product' AND (wp1_posts.post_status = 'publish') 

pay attention to wp1_posts.post_type and get a hint. Be flexible on yourself and try to debug. the results above are shown without $where .= " AND ($wpdb->posts.post_type = 'product') "; .

+4
source share

All Articles