我正在使用this code 从wordpress/woocommerce网站搜索产品
我的要求是URL类似于“http://localhost/wp/?s=D34&post_type=product“而s=D34
是搜索字符串。
当用户搜索字符串时。将从所有默认字段+产品中搜索数据custom filed
. 以下代码适用于http://localhost/wp/?s=D34 但是什么时候&post_type=product
与url连接,然后显示
Code is given 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 to prevent distinct 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\' );
那么,需要进行哪些修改?
URL
http://localhost/wp/?orderby=price&post_type=product 工作正常
但是有什么问题吗http://localhost/wp/?s=D34&post_type=product
Wordpress版本为16和4.4。已安装Woocommerce插件
在这两种情况下,查询都是
Case 1: http://localhost/wp/?s=D34&post_type=product
Case 2: http://localhost/wp/?s=D34
AND wp_posts.post_type = \'product\' AND (wp_posts.post_status = \'publish\') AND (((wp_posts.post_title LIKE \'%D34%\') OR (wp_postmeta.meta_value LIKE \'%D34%\') OR (wp_posts.post_content LIKE \'%D34%\'))) AND (wp_posts.post_password = \'\') AND wp_posts.post_type IN (\'post\', \'page\', \'attachment\', \'product\') AND (wp_posts.post_status = \'publish\') AND (wp_posts.post_type = \'product\')