Woocommerce – Hide ‘Hidden’ products from site search

This problem has been around for a while, and none of the resources that I could find online gave me any sort of idea  how it could be done. The worst result that I found was this one, where a representative of WooThemes themselves said that this couldn’t be done… That led me to believe that they were right, and it really couldn’t be done. Admittedly, that answer was given in 2012, so it’s a few years past now, so I wanted to see if anything had changed since then.

That got me thinking – if I could control the search query, why couldn’t I filter out the values? After all, it’s only a meta field, and I know that I can filter those for searches.

So I looked into it, and I came up with this small bit of code that actually works.

add_action ('pre_get_posts', 'catacuastic_hide_hidden_products');

function catacaustic_hide_hidden_products ($query) {
    if (is_search () && !is_admin ()) {
        $query->query_vars ['meta_query'] = array (
            'relation' => 'OR',
            array (
                'key' => '_visibility',
                'compare' => '!=',
                'value' => 'hidden'
            ),
            array (
                'key' => '_visibility',
                'compare' => 'NOT EXISTS'
            )
        );
    }
};

What I’ve done there is give it two directives, and said that if either of them don’t fit, don’t include that record. First, I search to see if the visibility value is not set as ‘hidden’. Second, I check if the visibility value is actually there, and if it’s not there (remember only products have this set) then include the record.

So, the “solution” from Woo themselves is indeed now wrong, and we can filter out hidden products from the standard WordPress search!

2 thoughts on “Woocommerce – Hide ‘Hidden’ products from site search”

  1. The guys over at WooCommerce have really irritated me lately. I recently got hit with a $10k bill for a few plugins I own (completely out of the blue). Was you aware that they have doubled their prices by 50 on all annual subscription renewals – without explaining anything to their customers! Seems pretty strange. Did this have any affect on you?

    1. I find it hard to believe that you had a bill for $10,000 – Unless you’re subscribed to EVERY extension that they make.

      Oh.. Yes you are… and you’re giving them away “free”. But not really free, when you are trying to get people to purchase memberships.

      So you’re complaining because you’re being charged for the things that you’re charging people for? That’s just so hypocritical of you. How about you do the right thing, and not try to profit form their work while you’ve done nothing to earn that money?

Leave a Reply

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