Error in searching specific post_type form post_type column using WooCommerce product search Error in searching specific post_type form post_type column using WooCommerce product search wordpress wordpress

Error in searching specific post_type form post_type column using WooCommerce product search


try this

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've used print_r ($where); to check what value does $where contains, you will see something like these...

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')

take note of wp1_posts.post_type and get a hint.. be flexible on yourself and try to debug. above are results without the $where .= " AND ($wpdb->posts.post_type = 'product') "; though.