I am trying to query a list with all Categories, Subcategories and Posts of a custom post type.

e.g.:

Maincat1
  -Subcat1
     -Post1
     -Post2
  -Subcat2
  -Subcat3
Maincat2
  -Subcat3
     -Post3
Maincat3
  -Post4

I’m getting mad trying to accomplish that. I tried out so many things that I found on the web but I can’t figure out how to get it to work.

What I have so far (working), All Categories + Subcategories but i don’t get it how to query the posts from the Categories / Subcategories.

<?php

$orderby      = 'name';
$show_count   = 1;      // 1 for yes, 0 for no
$pad_counts   = 1;      // 1 for yes, 0 for no
$hierarchical = 1;      // 1 for yes, 0 for no
$title="";
$empty        = 0;

$args = array(
  'taxonomy'     => 'kategorie',
  'orderby'      => $orderby,
  'show_count'   => $show_count,
  'pad_counts'   => $pad_counts,
  'hierarchical' => $hierarchical,
  'title_li'     => $title,
  'hide_empty'   => $empty
);

?>
<?php

$parent_cat_arg = array('hide_empty' => false, 'parent' => 0 );
$parent_cat = get_terms('kategorie',$parent_cat_arg);//category name

foreach ($parent_cat as $catVal) {

    echo '<h3>'.$catVal->name.'</h3>'; //Parent Category

$args = array('post_type' => 'Dokumente',
        'tax_query' => array(
            array(
                'taxonomy' => 'kategorie',
                'field' => 'slug',
                'terms' => $custom_term->slug,
            ),
        ),
     );


$my_query = new WP_Query( $args );

if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <ul><li><a href="#" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a><li></ul><?php
    endwhile;
}

wp_reset_query();


    $child_arg = array( 'hide_empty' => false, 'parent' => $catVal->term_id );
    $child_cat = get_terms( 'kategorie', $child_arg );

    echo '<ul>';
        foreach( $child_cat as $child_term ) { 
            echo '<li>'.$child_term->name . '</li>'; //Child Category



$my_query = new WP_Query( $args );

if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <ul><li><a href="#" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a><li></ul><?php            
    endwhile;
}

wp_reset_query();
        }
    echo '</ul></li>';

}
?>
</div>
<?php

}

add_shortcode('kategorien', 'get_mylist' );

I also have the code to query the posts (at least i think it works) – but i don’t know how to combine them.

$custom_terms = get_terms('kategorie');

foreach($custom_terms as $custom_term) {
wp_reset_query();

$args_slugs = array('post_type' => 'Dokumente',
        'tax_query' => array(
            array(
                'taxonomy' => 'kategorie',
                'field' => 'slug',
                'terms' => $custom_term->slug,
            ),
        ),
     );


     $loop = new WP_Query($args_slugs);
     if($loop->have_posts()) {
        while($loop->have_posts()) : $loop->the_post();
            echo '<ul><li><a href="'.get_permalink().'">'.get_the_title().'</a></li></ul><br>';
        endwhile;
     }
}

I really hope someone can help me… And yes I already used the search and found similar questions but none of the answers worked for me.

Best,
NixXxon

2 Answers
2

@niko Please, find your required code, it will give you exact output you were seeking for. I think you were doing all right except in the final output you were missing the right structure.

Also, I recommand not to assign posts to the parent category as this structure not support that. However, I think with minor tweaks in following that can also be acheived.

<?php   
    $taxonomy = 'testimonial-category';
    $postType="testimonial";
    $terms = get_terms(['taxonomy' => $taxonomy, 'orderby' => 'term_id', 'parent' => 0, 'hide_empty' => false]);
?>
<div class="">
<?php
    foreach($terms as $term){
        echo '<h3>' . $term->name . '</h3>';
        $childTerms = get_terms(['taxonomy' => $taxonomy, 'orderby' => 'term_id', 'parent' => $term->term_id, 'hide_empty' => false]);

        foreach($childTerms as $childTerm)
        {
            $posts = get_posts(array('post_status' =>'publish','post_type' => $postType,
                    array(
                        'taxonomy' => $taxonomy,
                        'field' => 'term_id',
                        'terms' => $childTerm->term_id,
                    ),));
            ?>
            <div class="add-accordion">
                <h3><?php echo $childTerm->name ?></h3>
                <div class="add-accordion">
                    <?php foreach($posts as $post){ ?>
                        <h3><?php echo $post->post_title ?></h3>
                        <div class="">
                            <?php echo get_the_content($post->ID) ?>
                        </div>
                    <?php } ?>
                </div>
            </div>
            <?php
        }
    }
?>
</div>
<script>
    jQuery(function(){
        jQuery('.add-accordion').accordion({
            collapsible: true,
            heightStyle: "content",
            active: false,
            animate: 500,
            icons: false
        });
    });
</script>
<?php wp_enqueue_script('jquery-ui-accordion'); ?>

Leave a Reply

Your email address will not be published. Required fields are marked *