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') "; .
Reigel
source share