Support » Plugin: Relevanssi - A Better Search » 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
     *
     * http://codex.wordpress.org/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
     *
     * http://codex.wordpress.org/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
     *
     * http://codex.wordpress.org/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.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Mikko Saari

    (@msaari)

    Those filters indeed do nothing for Relevanssi, but then again, you don’t need them – Relevanssi can search for custom fields without the help of any filters.

    So if your goal is to have a product search that includes custom fields, just go to Relevanssi indexing settings and make sure “Custom fields” is set to “all” or “visible” and rebuild the index – now you should have a product search that includes custom field contents as well (and frankly, Relevanssi does the custom field content searching better than that posts_where filter).

    If you do want to disable Relevanssi in particular searches, you can do that by removing two filters. The instructions are here.

    Thanks, I enabled the product custom post type and checked custom fields in the settings and it works great.

    Is there any way to search custom post types by custom fields on the admin side? I enabled admin search but it doesn’t seem to working.

    Next to this option it says “If checked, Relevanssi will be used for searches in the admin interface. The page search doesn’t use Relevanssi, because WordPress works like that.”

    I assume this means Relevanassi doesn’t apply here?

    Plugin Author Mikko Saari

    (@msaari)

    It’s quite possible it doesn’t work, the admin search is a bit wonky that way.

    However, Dashboard > Admin search is a search that is guaranteed to be powered by Relevanssi. It will find any posts by custom field, if the custom fields have just been indexed.

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.