• Resolved bhclocal

    (@bhclocal)


    Hello,

    I’m trying to get our product categories to appear 1st in search. Right now they are not appearing at all.

    Here’s what I’ve done/tried so far:
    1. Re-Indexed with product_cat selected.

    2. Added this code from the User Manual to functions.php makes sure product_cat was in:

    
    add_filter( 'relevanssi_modify_wp_query', 'rlv_include_product_cat' );
    function rlv_include_product_cat( $query ) {
    	if ( isset( $query->query_vars['product_cat'] ) ) {
    		$query->query_vars['tax_query'][] = array(
    			'taxonomy' => 'product_cat',
    			'field' => 'slug',
    			'terms' => $query->query_vars['product_cat'],
    			'include_children' => true,
    		);
    	}
    	return $query;
    }
    

    3. Then added this to re-ordered search display:

    add_filter('relevanssi_hits_filter', 'product_cat_first');
    function product_cat_first($hits) {
        $types = array();
     
        $types['page'] = array();
        $types['post'] = array();
        $types['product'] = array();
        $types['product_cat'] = array();
     
        // Split the post types in array $types
        if (!empty($hits)) {
            foreach ($hits[0] as $hit) {
                array_push($types[$hit->post_type], $hit);
            }
        }
     
        // Merge back to $hits in the desired order
        $hits[0] = array_merge($types['product_cat'],$types['product'],$types['page'], $types['post']);
        return $hits;
    }

    Am I missing something?

    Thanks in advance. 🙂

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

    (@msaari)

    Yeah, you’re missing something – the free version can’t return categories as search results, that’s a Premium feature. In the free version, indexing the product_act category just means that when you search for a category name, you’ll find all posts in that category. To get the category itself, you need Premium.

    With Premium, you don’t need the code in step 2, the code in step 3 is enough.

    Thread Starter bhclocal

    (@bhclocal)

    Oooh! That’s the detail I missed. 🙂

    Guess I’ll be putting a request to get the Premium version for this project.

    Really appreciate your quick reply. Relevanssi is awesome. 🙂

    Thank you!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Product Categories in Search’ is closed to new replies.