1

I'm trying to hide products that are not in any category (product_cat) from the search results.

I've been trying to figure out how I could modify the search query to exclude these products, but I can't figure it out.

The problem I'm running into is that uncategorized products are not actually being put in a category called uncategorized (like posts are). They simply don't have categories.

Printing terms for products with categories:

Array( [0] => WP_Term Object ( ... ))

Printing terms for products without categories:

nothing...

So how can I target the uncategorized products in the query?

// HIDE UNCATEGORIZED PRODUCTS FROM SEARCH RESULTS
function sp_pre_get_posts( $query ) {
    if (! is_admin() && $query->is_main_query() && $query->is_search()) {
        $query->set( 'post_type', array( 'product' ) );
        $tax_query = array(
            array(
                'taxonomy' => 'product_cat',
                'field'   => 'term_id',
                'terms'   => '',
                'operator' => 'NOT IN',
            ),
        );
        $query->set( 'tax_query', $tax_query );
    }
}
add_action( 'pre_get_posts', 'sp_pre_get_posts' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.