Search code examples
jquerywordpresswoocommercewordpress-themingwoocommerce-theming

How can i query WooCommerce product by pricing of $00 product?


I have a custom query inside my template, I want to show product by price, I want to query free products, and only show $0 products on my home page.


Solution

  • $args = array(
                'post_type'      => 'product',
                'numberposts'      => -1,
                'meta_query'     => array(
                    'relation' => 'OR',
                    array(
                        'key'           => '_regular_price',
                        'compare' => 'NOT EXISTS'
                    ),
                    array( 
                        'key'           => '_price',
                        'compare' => 'NOT EXISTS'
                        
                    ),
                    array( 
                        'key'           => '_regular_price',
                        'compare' => '=',
                        'value' => 0,
                    ),
                    array( 
                        'key'           => '_price',
                        'compare' => '=',
                        'value' => 0,
                    )
                )
            );
            
            $products =  get_posts( $args );