Hide Products Without a Price From the Shop
Keeps unfinished products that have no price yet out of the shop, category pages and product searches until you add one.
Opens WPIDE on
Code
$wpide_require_price = function ( $query ) {
$meta_query = (array) $query->get( 'meta_query' );
$meta_query[] = array(
'key' => '_price',
'value' => '',
'compare' => '!=',
);
$query->set( 'meta_query', $meta_query );
};
add_action( 'woocommerce_product_query', $wpide_require_price );
add_filter( 'woocommerce_shortcode_products_query', function ( $args ) {
$args['meta_query'] = isset( $args['meta_query'] ) ? (array) $args['meta_query'] : array();
$args['meta_query'][] = array( 'key' => '_price', 'value' => '', 'compare' => '!=' );
return $args;
} );
Notes
Products stay reachable by direct link, and variable products are listed when any variation has a price.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.