1

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)
  • Are you trying to loop through all parent categories and list products per child category of each parent category? – darrinb Oct 21 '15 at 22:52
  • You shouldn't be calling wp_reset_query, instead call wp_reset_postdata inside the if statement at the very end of your loop – Tom J Nowell Oct 22 '15 at 0:40
2

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;

        endif;

    }

}

wp_reset_query();

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.
  • thank you very much! this kinda works, but it's not quite what I meant. i tried to edit my question for clarification. – tarpier Oct 22 '15 at 20:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.