I have at least 4 Parent Categories and each parent category has a sub-category.
The categories in WordPress looks like this:
Lidingö (Parent Category)
- Direktåtkomst Förrådslänga(SubCategory)
- 7 kvm (product)
- 6 kvm (product)
- Entréplan (SubCategory)
- 1 kbm (product)
- 1.5 kvm (product)
- Direktåtkomst Förrådslänga(SubCategory)
Nacka (Parent Category)
- Sample(SubCategory)
- aa (product)
- bbb (product)
- Sample(SubCategory)
I want to query the products in WordPress and they should be grouped by categories.
This is my current code:
<?php
$args = array(
'post_type' => 'product',
array(
'taxonomy' => 'product_cat'
),
'posts_per_page' => 6,
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
echo woocommerce_template_single_title();
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
I think the code above is not correct because the same products are being displayed for every pagination.
Do you have any idea what is the correct query to group all woocommerce products by category? Thanks