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


Try something like this:

<?php
$parent_terms = get_terms(
    'name_of_your_taxonomy',
    array(
        'parent' => 0, 
    )
);

foreach ( $parent_terms as $parent_term ) {

    $child_terms = get_terms(
        'name_of_your_taxonomy'
        array(
            'child_of' => $parent_term->term_id,
        )
    );
    
    foreach ( $child_terms as $child_term ) {
    
        $args = array(
            'post_type' => 'product',
            'tax_query' => array(
                array(
                    'taxonomy'      => 'name_of_your_taxonomy',
                    'field'         => 'slug',
                    'terms'         => $child_term->slug,
                ),
            ),
        );
    
        $loop = new WP_Query($args);
        
        if ( $loop->have_posts() ) :
        
            while ( $loop->have_posts() ) :
                $loop->the_post();
            
                echo '<h3>' . $parent_term->name . '</h3>';
                echo '<h4>' . $parent_term->name . '</h4>';
                echo get_the_title();

            endwhile;
            wp_reset_postdata();
        endif;
    }
}

Essentially what you're doing is:

  1. Getting all parent terms;
  2. Looping through the parent terms and getting the children of each term;
  3. Looping through the children terms and getting posts associated with the child.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.