Skip to main contentSkip to footer

While Relevanssi Premium can return taxonomy terms as search results, sometimes sending the searcher directly to the category archive is better. This is especially true for WooCommerce sites, where displaying taxonomy terms in search results pages is complicated.

Relevanssi Premium has a redirect feature you can use to redirect searches to specific pages. This is a good solution, but manually maintaining the redirects can be a chore if there are many categories. Fortunately, this is something that can be automated.

Here’s one solution:

add_action( 'saved_term', function() {
    $redirects    = get_option( 'relevanssi_redirects', array() );
    $product_cats = get_categories( 'taxonomy=product_cat' );

    $existing_redirects = wp_list_pluck( $redirects, 'url' );
    foreach ( $product_cats as $cat ) {
        $url = get_category_link( $cat->term_id );
        if ( ! in_array( $url, $existing_redirects, true ) ) {
            $redirects[] = array(
                'url'     => $url,
                'query'   => $cat->name,
                'partial' => false,
                'hits'    => 0,
            );
        }
    }

    update_option( 'relevanssi_redirects', $redirects );
} );

This function runs on the saved_term action hook, which triggers whenever a taxonomy term is saved. It checks your redirects and adds missing product_cat category redirects. This will make sure your redirection settings are always up to date. To create the first redirects, you need to add this function to your site and then save a product category term.

This does not remove redirects for non-existing categories, so that is something you need to maintain manually.

Your account

Not logged in. Log in to see your license details.

Search

Popular Resources

Jetpack Protect and Relevanssi Premium vulnerabilities

At the moment, Jetpack Protect claims Relevanssi Premium has several vulnerabilities. These are all false positives. I am not aware of any current vulnerabilities in Relevanssi Premium. The vulnerabilities Jetpack Protect reports are old vulnerabilities. They have applied to Relevanssi Premium, but I fixed them long ago. They appear in……Snowball Stemmer, which is even more absurd and shows how little sense there is in these reports. I’ve reported this, and hopefully, Jetpack Protect will stop reporting these vulnerabilities. Meanwhile, you don’t have to worry about the Relevanssi vulnerabilities Jetpack Protect reports for Relevanssi Premium. It’s worth worrying if Jetpack…

Voice Search
Modern search is about accessibility and speed. Relevanssi now includes a Voice Search feature that allows your visitors to search…
Yoast SEO

Starting from versions 2.4.0 (Premium) and 4.3.0 (Free), Relevanssi doesn’t index posts that have been marked “noindex” in the Yoast SEO settings (“Allow search engines to show this Post in search results?”). If the “noindex” setting is in the default value or set to explicit “yes”, Relevanssi indexes the post….

Related Posts:

Currently there are no related posts available.

Comment Section:

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed