• Resolved rckychng

    (@rckychng)


    Saw this post from a while back and wanted to know how I would account for grandparent categories as well.

    Also, is it possible for Relevanssi to account for a parent category if the child category is searched? For example using the original example, Car > Wiper Blades, if a product is only categorized as Car, a search of Wiper Blades would show any results with products categorized with Car as well.

    • This topic was modified 2 years, 9 months ago by rckychng.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Mikko Saari

    (@msaari)

    This version will also index the grandparent categories:

    add_filter('relevanssi_content_to_index', 'rlv_parent_categories', 10, 2);
    function rlv_parent_categories($content, $post) {
    	$categories = get_the_terms($post->ID, 'category');
    	if (is_array($categories)) {
    		foreach ($categories as $category) {
    			if (!empty($category->parent)) {
    				$parent = get_term($category->parent, 'category');
    				$content .= $parent->name;
    				if (!empty($parent->parent)) {
    					$grandparent = get_term($parent->parent, 'category');
    					$content .= $grandparent->name;
    				}
    			}
    		}
    	}
    	return $content;
    }

    Your second question is possible, but I’d really consider that well – things do get confusing when the search returns results that seem unrelated to the search query.

    But it’s possible. I think the easiest approach is to use a relevanssi_content_to_index filter function that checks the categories the post has, fetches their children categories with get_term_children() and index the names of the children categories for the post (this example may be helpful) so that every post that’s in the category “Car” will also have the words “Wiper blades” and all other child category names indexed for it.

    Thread Starter rckychng

    (@rckychng)

    Thanks Mikko!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Account for grandparent categories’ is closed to new replies.