Skip to main contentSkip to footer

Relevanssi by default shows out-of-stock and excluded from catalogue WooCommerce products in the search results, but hides those set to excluded from search (before 2.2.2 and 4.1.2 the default behaviour was to show all products). It is quite easy to make Relevanssi not display hidden products in the results. The best way is to unindex the products so that they don’t appear in the index at all.

How that is done, depends on the version of WooCommerce and Relevanssi you’re using.

Relevanssi 4.0.9 / Premium 2.1.5 and WooCommerce 3

Versions 4.0.9 and 2.1.5 introduced a better way to block excluded products from the search. This method leads to much faster indexing times. Add this to your site:

add_filter( 'relevanssi_woocommerce_indexing', 'rlv_woocommerce_filter' );
function rlv_woocommerce_filter( $blocks ) {
    $blocks['outofstock']           = true;
    $blocks['exclude-from-catalog'] = true;
    $blocks['exclude-from-search']  = true;
    return $blocks;
}

There’s one key for each product visibility level, set the ones you want to block from the search to true.

Older Relevanssi versions and WooCommerce 3

WooCommerce 3 uses taxonomy terms to handle visibility. You can create a relevanssi_do_not_index filter function for these:

add_filter( 'relevanssi_do_not_index', 'rlv_wc3_hidden_filter', 10, 2 );
function rlv_wc3_hidden_filter( $block, $post_id ) {
    if (
      has_term(
        array(
          'exclude-from-catalog',
          'exclude-from-search',
          'outofstock'
        ),
        'product_visibility',
        $post_id
      )
    ) {
      $block = true;
    }
    return $block;
}

This code will unindex products that are set to be excluded from the catalogue, excluded from search or that are out of stock. If you want to keep out of stock items in the search results, just remove that line from the function.

The changes take effect once you rebuild the index. Do note that if you change the product visibility without saving the post, the changes won’t be reflected in the index until the post is saved or the index is otherwise rebuilt.

WooCommerce 2

Earlier versions of WooCommerce use a custom field to control the visibility.

add_filter( 'relevanssi_do_not_index', 'rlv_wc2_hidden_filter', 10, 2 );
function rlv_wc2_hidden_filter( $block, $post_i d) {
  $visibility = get_post_meta ( $post_id, '_visibility', true );
  if ( 'catalog' === $visibility || 'hidden' === $visibility ) {
    $block = true;
  }
  return $block;
}

The visibility setting has four values: “visible”, “catalog”, “search” and “hidden”. “Visible” means it’s visible everywhere, “hidden” means it’s hidden everywhere. “Catalog” means the product is visible in shop pages and category pages, but not search results. “Search” means the product is visible in search results, but not on the shop page or category pages. This function matches that definition.

Again, you need to rebuild the index to see the effects of this.

See more WooCommerce tips.