Hidden woocommerce products still showing up in search results [closed]

This code can’t be added anywhere you want, and it can’t be added to functions.php or any other php file like this. This is a element for the array used to create the WordPress query object. It has to be added via WP_Query class or get_posts and query_posts functions, or via the filter to modify the main page query.

But, without knowing more on how your search template works, there is no way to provide the help with this. If you work with classic search template, this is the code that will apply the taxonomy filter you need to use, and you can add this to the functions.php:

add_action('pre_get_posts', 'wpse_187444_search_query_pre');

function wpse_187444_search_query_pre($query) {
    if ($query->is_search() && $query->is_main_query()) {
        $tax_query = $query->get('tax_query', array());

        $tax_query[] = array(
            'taxonomy' => 'product_visibility',
            'field'    => 'name',
            'terms'    => 'exclude-from-catalog',
            'operator' => 'NOT IN',
        );

        $query->set('tax_query', $tax_query);
    }
}

But, this might not work for you, it depends on your search template and the way search results are queried.

Leave a Comment