Posted on

WPML: Category exclusions

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;
}

Leave a Reply

Are you a Relevanssi Premium customer looking for support? Please use the Premium support form.

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