Breaks WooCommerce product search
-
Strange issue. I have a site that uses WooCommerce. I’m using Relevanasi on the general site search form and then I have WooCommerce specific search form that only searches for products.
I have code in my functions file that extends the search to include meta fields so that the search results for the products can find things like the product reference number etc.
When I have Relevanasi enabled, the WooCommerce search doesn’t include the meta fields. It displays no results for a search on a meta field that is and exact match. When I disable Relevanasi, it works again.
The extra code I’ve inserted is as follows:
** * Extend WordPress search to include custom fields * * https://adambalee.com */ /** * Join posts and postmeta tables * * https://codex.www.ads-software.com/Plugin_API/Filter_Reference/posts_join */ function cf_search_join( $join ) { global $wpdb; if ( is_search() ) { $join .=' LEFT JOIN '.$wpdb->postmeta. ' ON '. $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id '; } return $join; } add_filter('posts_join', 'cf_search_join' ); /** * Modify the search query with posts_where * * https://codex.www.ads-software.com/Plugin_API/Filter_Reference/posts_where */ 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 ); } return $where; } add_filter( 'posts_where', 'cf_search_where' ); /** * Prevent duplicates * * https://codex.www.ads-software.com/Plugin_API/Filter_Reference/posts_distinct */ function cf_search_distinct( $where ) { global $wpdb; if ( is_search() ) { return "DISTINCT"; } return $where; } add_filter( 'posts_distinct', 'cf_search_distinct' );
Is there a way to have Relevanasi working for the site search but keep the Products search separate and not conflict?
I assume Relevansi is overwriting my search code on the product search page as well.
- The topic ‘Breaks WooCommerce product search’ is closed to new replies.