Skip to main contentSkip to footer

Relevanssi category exclusion setting doesn’t work properly with WPML. Here’s a bit of code from Srdjan Jocić from OnTheGoSystems that fixes it. Just add this to your theme functions.php.

add_filter( 'relevanssi_search_filters', 'wpml_relevanssi_include_exclude_cats_fix' );
function wpml_relevanssi_include_exclude_cats_fix( $args ) {
    if ( array_key_exists( 'tax_query', $args ) && did_action( 'wpml_loaded' ) ) {
        foreach ( $args['tax_query'] as &$query ) {
            if ( isset( $query['terms'] ) && isset( $query['taxonomy'] ) ) {
                $terms = null;

                if ( is_string( $query['terms'] ) ) {
                    $terms = explode( ',', $query['terms'] );
                } elseif ( is_array( $query['terms'] ) ) {
                    $terms = $query['terms'];
                }

                if ( $terms ) {
                    foreach ( $terms as &$term_id ) {
                        $term_id = apply_filters( 'wpml_object_id', $term_id, $query['taxonomy'] );
                    }
                    $query['terms'] = $terms;
                }
            }
        }
    }

    return $args;
}

Your account

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

Search

Popular Resources

Searching between dates
If you want to have a search with a date range, that’s possible with Relevanssi. It takes a bit of…
Oxygen and Ninja Tables
Relevanssi has support both for Oxygen and Ninja Tables, but the way the Ninja Tables support is done, it’s not…
FacetWP
Relevanssi is generally well compatible with FacetWP, which provides advanced filtering and faceting capabilities for the search. There are, however,…

Related Posts:

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