I have a WP_Query
to pull and display a list of WooCommerce Product variations filtered by global attributes. This works, but I would like to custom sort the results by ‘menu_order‘.
But this doesn't work for 'post_type' => 'product_variation'
. Probably because ‘menu_order‘ is set on a product level.
('post_type' => 'product'
does work but I need the variations)
$wc_query_params = array(
'post_type' => 'product_variation',
'posts_per_page' => -1,
'numberposts' => -1,
'post_status' => 'publish',
'meta_query' => array(array(
//filter variations on attribute name
'key' => 'attribute_pa_color',
'value' => 'blue',
)),
'order' => 'ASC',
'orderby' => 'menu_order',
);
$wc_query = new WP_Query($wc_query_params);
if ($wc_query->have_posts()) :
while ($wc_query->have_posts()) :
$wc_query->the_post();
//results
endwhile;
wp_reset_postdata();
endif;
Is there a way to sort product variations based on their parent product ‘menu_order‘? Or is there another way to custom sort product variations?