I'm building custom endpoint REST API and I need the same format wooCommerce gave for its post type ( product ), using the WP_Query()
class.
I tried the wc_Product_Query
and for some reason not working with my tax_query
, So instead, I start using the Wordpress query WP_Query
and works very well but the wooCommerce has much data than just default post like price, categories, images ...etc
So How I implement those data on my query?
My code:
function hash_realated_products() {
$query = new WP_Query( array(
'post_type'=> 'product',
'per_page' => 10,
'status' => 'publish',
'orderby' => 'rand',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_tag',
'field' => 'name',
'terms' => array('component'),
'operator' => 'NOT IN',
'compare' => 'LIKE'
),
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => array(17), // When you have more term_id's seperate them by komma.
'operator' => 'IN'
)
)
));
$products = $query->get_posts();
wp_reset_postdata();
return $products;
}