Posted on

WooCommerce: Hidden products in search

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 (
    ) {
      $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.

23 comments WooCommerce: Hidden products in search

  1. This code snippet is causing my index building to fail. The indexing process begins to index less than the specified amount of documents, until it locks up altogether, with a little more than half the documents indexed.

    As soon as I remove this code from my functions.php, I can continue to build the index again. Any ideas?

    1. Reduce the number of posts to index, then. This adds a new database three extra database queries per post, which can slow down – especially if it’s a slow query.

      There’s probably a faster way to do this, perhaps it’s possible to fetch a list of all excluded posts at once and then check against that list.

  2. Hello. I’ve coped and pasted your code and added to functions.php. Then I rebuild the index. All of my products that are “hidden” are still showing in the search. I’m using the latest woocommerce version. Thank-you.

    1. If you add

      var_dump(has_term(‘exclude-from-search’, ‘product_visibility’, $post_id)); exit();

      before the “return $block;” line in the function and go save a hidden post, what does it print out?

          1. Yes, absolutely. I have a product that is marked as “hidden” for catalog visibility. Relevanssi is active (other changes I’ve made in relevanssi are active). I have the code with the var_dump before the return $block. I go to a “hidden” product and re-save it. I don’t see anything. Here’s the code:

            add_filter(‘relevanssi_do_not_index’, ‘rlv_wc3_hidden_filter’, 10, 2);
            function rlv_wc3_hidden_filter($block, $post_id) {
            if (has_term(‘exclude-from-catalog’, ‘product_visibility’, $post_id)) $block = true;
            if (has_term(‘exclude-from-search’, ‘product_visibility’, $post_id)) $block = true;
            if (has_term(‘outofstock’, ‘product_visibility’, $post_id)) $block = true;
            var_dump(has_term(‘exclude-from-search’, ‘product_visibility’, $post_id)); exit();
            return $block;

          2. There’s an exit(); in the code, so it should simply halt the process. If that doesn’t happen when you save a post, then the code is not running at all. This filter should trigger every time you save a post. Does it trigger when you save a product that is not hidden from Relevanssi? Or another post type?

          3. No, it does not trigger at all when saving a non-hidden product or any other post types. Do you think I have a plugin or other code in functions.php that is stopping this? Heartbeat control???

          4. And you’re 100% sure that Relevanssi is set to index posts? This filter should trigger each and every time a post is indexed. If Relevanssi is working, then I’d look at making sure this filter is somewhere that is being run. You’re sure it’s in the right file?

          5. total rookie mistake, I didn’t close my comment in functions.php properly. The code works fine. Thanks so much for your support. I will plan to buy the premium version and leave a positive comment on wordpress. Thank-you.

          6. Hey Mikko,

            First of all, thanks for a great plugin!
            I’m having the same issue, where products are still indexing in search after adding this code and rebuilding the index several times.

            I tried adding your break code, and as soon as I updated a post, it output bool(true); Is this the expected result? This means the code seems to be running fine, right?

            Would appreciate any help you have 🙂

          7. Yes, that would be the expected result and mean that the products are being blocked. If they’re not, that would suggest the search may not be powered by Relevanssi. Are you sure it is?

          8. I’m almost certain – we’ve made lots of changes to Relevanssi that shows up in search. Any other thoughts? I can share the website if that helps.

          9. Won’t help much, can’t really tell much from the front end alone. I’d have to look at the source code, see how it runs and what happens in the database. But if the “do not index” function is returning “true”, then the posts simply should not appear in the database (unless there’s another function after that that sets the value to “false” again).

          10. Just doing a bit more troubleshooting, and it seems that Relevanssi is running in the search dropdown, but not once we go to the search results page after hitting enter. Any ideas how to switch it?

          11. Usually it’s the other way around, but you might want to investigate your search results template – is it using the default query or doing a custom query?

          12. I enabled the Logs for Relevanssi, and it does seem to be going through it. Any further thoughts? Could it somehow be getting cached?

          13. If you have an extra query in your search results template, that’s what would happen: Relevanssi runs, then your search results template does the extra query and overwrites the Relevanssi results with new results. Can you show me the template?

  3. It took me some time, but I finally figured out how to exclude hidden products while using the latest version of Woo and Relevanssi:

    add_filter( ‘relevanssi_woocommerce_indexing’, ‘rlv_woocommerce_filter’ );
    function rlv_woocommerce_filter( $blocks ) {
    $blocks[‘product_visibility’] = true;
    $blocks[‘exclude-from-catalog’] = true;
    $blocks[‘exclude-from-search’] = true;
    return $blocks;

    PLEASE add this as an option in Relevanssi, for when Woo is installed.

    1. Well, clicking “Help” in Relevanssi admin settings page and checking the WooCommerce help section there might’ve cut the time a bit – the information is there as well. But I’ll consider adding this as an option.

  4. I have added snippet in my function.php
    add_filter( ‘relevanssi_woocommerce_indexing’, ‘rlv_woocommerce_filter’ );
    function rlv_woocommerce_filter( $blocks ) {
    $blocks[‘outofstock’] = true;
    var_dump(has_term(‘outofstock’, ‘product_visibility’, $post_id)); exit();
    return $blocks;

    and if saving product i got bool(false)
    Product itself is out of stock

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 *