How to exclude products from being listed

Sometimes you need to exclude products from being viewed in WooCommerce catalog, to hide WooCommerce products.

Here is the code you can use to do that easily.

 

// Exclude products from being displayed in loop - by meta field
function wpm_exclude_featured_posts($query) {
	
	if(is_admin() || !isset($_GET['groups'])){
		return $query;
	}
	
    $exclude = array();  //Create empty array for post ids to exclude
    if ( $query->is_main_query() ) {
            $featured = get_posts(array(
                'post_type' => 'product',
                'meta_query' => array(
                    array(
                        'key' => 'product_type_membership',
                        'value' => 'manuals_product',
                        'compare' => '==',
                    ),
                ),
                'posts_per_page' => -1
            ));

            foreach($featured as $hide) {
                $exclude[] = $hide->ID;
            }   

            $query->set('post__not_in', $exclude);
        }
}

add_filter( 'pre_get_posts', 'wpm_exclude_featured_posts' );

Get in touch!

Send us a message, we are
happy to provide assistance or a project quote.