Home » WordPress » Custom wooCommerce product Query

Custom wooCommerce product Query

Querying WooCommerce products using PHP
here bellow code get all products in live-experiences category limit is set to 20

$query = new WC_Product_Query(array(
            'limit' => 20,
            'status' => 'publish',
            'tax_query' => array(array(
                'taxonomy' => 'product_cat',
                'field' => 'slug',
                'terms' => 'live-experiences',
            )),
        ));


$products = wc_products_array_orderby($query->get_products(), 'price', 'DESC');

if you want to use meta filter, you can modify above code as bellow

$query = new WC_Product_Query(array(
            'limit' => 20,
            'status' => 'publish',
            'tax_query' => array(array(
                'taxonomy' => 'product_cat',
                'field' => 'slug',
                'terms' => 'live-experiences',
            )),
        ));

        // meta filter by custom filed value
        $query->set('meta_key', 'private_event');
        $query->set('meta_value', 'Yes');
        $query->set('meta_compare', '=');


       $products = wc_products_array_orderby($query->get_products(), 'price', 'DESC');
Duminda Wijerathna

An all round web designer building websites Over 18 years. Interested in achieving a suitable placement in the field of IT in a growth oriented organization which offers diverse job responsibilities in order to utilize and improve my skills, Knowledge and experience.

Leave a Comment

Your email address will not be published. Required fields are marked *