Display custom post type in hierarchical order with get_terms
How can I display a custom post type of products with a custom taxonomy (product category) in a list, in hierarchical order?
I'd like to get them in this order:
h3.parentcategory 1
-h4.childcategory 1
-li.product of childcategory 1
-li.product of childcategory 1
-h4.childcategory 2
-li.product of childcategory 2
-li.product of childcategory 2
h3.parentcategory 2
-h4.childcategory 1 of parent 2
Unfortunately I can't wrap my head around the second level (childcategory 1 2). I'm using this code to display category and product:
$custom_terms = get_terms( 'productcategory' );
foreach ( $custom_terms as $custom_term ) {
wp_reset_query();
$args = array(
'post_type' = 'product',
'tax_query' = array(
array(
'taxonomy' = 'productcategories',
'field' = 'slug',
'terms' = $custom_term-slug,
'orderby' = 'term_group',
),
),
);
$loop = new WP_Query( $args );
if ( $loop-have_posts() ) {
echo 'h3' . $custom_term-name . '/h2';
while ( $loop-have_posts() ) : $loop-the_post();
get_the_title();
endwhile;
}
}
I need help figuring out the nested levels of this list.
I'm editing this to try to clarify: thanks so far for the help guys. and sorry for being a noob in posting...
What i'm trying to do is list products that are part of a subcategory, these subcategories can then be part of a big family (Maincategory). I'm trying to group subcategories under a Main category.
vegetables (Main)
-tomatoes (sub)
-red tomatoe sweet (product)
-pink tomatoe sour (product)
-cucumber (sub)
-some kind of cucumber(product)
fruits (Main)
-apple(sub)
-sweet apple (product)
-Golden delicious (product)
Topic hierarchical custom-taxonomy custom-post-types Wordpress
Category Web