Excluding posts by taxonomy

Relevanssi has a category exclusion feature built in, but that only works for basic categories and not any other taxonomies. You can use tax_query to restrict the searches as well, but that’s more complicated than adjusting the setting in Relevanssi settings.

However, if the exclusion is permanent, it’s best done in the indexing level. That way the excluded posts are not taking space in the database. It’s really simple. Just add this code to your theme functions.php:

add_filter('relevanssi_do_not_index', 'rlv_exclude_cat', 10, 2);
function rlv_exclude_cat($exclude, $post_id) {
    if (has_term( TERM, TAXONOMY, $post_id )) $exclude = true;
    return $exclude;

where TERM is term name, slug or ID of the term you want to exclude, or an array if you plenty and TAXONOMY is the name of the taxonomy (for example “product_cat”, if you’re working with WooCommerce product categories). Then rebuild the index, and you shouldn’t see anything from the excluded category in the results.

  • Wen

    I tried this. But, I’m getting a PHP Warning error.

    Missing argument 2 for rlv_exclude_cat().

    Could you tell me how to fix it?