Display Posts and from the Childs of a parent Term.

Category Parent
Category Child Name
1
2
3
Category Child Name
1
2
3

Is there a way to do this with my custom taxonomy?

im using this code

<?php  $cats = get_categories('taxonomy=material&child_of='.get_query_var('cat')); 

    foreach ($cats as $cat) :

    $args = array(
    'posts_per_page' => -1, // max number of post per category
	'post_type' => 'lesson',
    'category__in' => array($cat->term_id)
    );
    $my_query = new WP_Query($args); 

        if ($my_query->have_posts()) :
        echo '<h3>'.$cat->name.'</h3>';

        while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <?php /*general loop output; for instance: */ ?>
        <a href="<?php the_permalink() ?>"><?php the_title(); ?></a>    <br />  

        <?php endwhile; ?>

        <?php else :
        echo 'No Posts for '.$cat->name;
        endif; 

    endforeach; ?>
  • Sajid

    Hi @Carissa

    Hope you are doing good today :slight_smile:

    The code looks good to me as far as you are getting the category id. I will make two changes here.

    Replace:
    get_query_var('cat')
    With:
    get_queried_object()->term_id;

    ...and

    'category__in' => array($cat->term_id)

    with (because we are sure there will be only one category ID all the time):
    'cat' => $cat->term_id

    I have also found follow ticket on stackoverflow on same topic that will be helpful for you too.
    http://stackoverflow.com/questions/12289169/how-do-i-get-current-taxonomy-term-id-on-wordpress

    Take care and have a nice day :slight_smile:

    Cheers, Sajid

  • Carissa Simmons
    <?php  $cats = get_categories('taxonomy=material&child_of='.get_queried_object()->term_id;); 
    
        foreach ($cats as $cat) :
    
        $args = array(
        'posts_per_page' => -1, // max number of post per category
    	'post_type' => 'lesson',
        'cat' => $cat->term_id
        );
        $my_query = new WP_Query($args); 
    
            if ($my_query->have_posts()) :
            echo '<h3>'.$cat->name.'</h3>';
    
            while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <?php /*general loop output; for instance: */ ?>
            <a href="<?php the_permalink() ?>"><?php the_title(); ?></a>    <br />  
    
            <?php endwhile; ?>
    
            <?php else :
            echo 'No Posts for '.$cat->name;
            endif; 
    
        endforeach; ?>

    Is this right can you check? Im getting a white blank page.

  • Carissa Simmons

    I got it working but every category shows all posts even if its not on that category

    <?php  $cats = get_categories(array(
    		'child_of'                 => get_queried_object()->term_id,
    		'taxonomy'                 => 'material'
    	));
    
        foreach ($cats as $cat) :
    
        $args = array(
        'posts_per_page' => -1, // max number of post per category
    	'post_type' => 'lesson',
        'cat' =>  $category->term_id
    
        );
        $my_query = new WP_Query($args); 
    
            if ($my_query->have_posts()) :
            echo '<h3>'.$cat->name.'</h3>';
    
            while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <?php /*general loop output; for instance: */ ?>
            <a href="<?php the_permalink() ?>"><?php the_title(); ?></a>    <br />  
    
            <?php endwhile; ?>
    
            <?php else :
            echo 'No Posts for '.$cat->name;
            endif; 
    
        endforeach; ?>
  • Sajid

    Hi @Carissa Simmons

    Hope you are doing good today :slight_smile:

    I got the culprit, we are trying to get custom post in custom taxonomy (term) but in wp_query we requesting WordPress category.

    In your code, replace:
    'cat' => $category->term_id`
    with:

    'tax_query' => array(
    		array(
    			'taxonomy' => 'material',
    			'field'    => 'term_id',
    			'terms'    => $category->term_id,
    		),
    	),

    Now your final code should look like this:

    <?php  $cats = get_categories(array(
    		'child_of'                 => get_queried_object()->term_id,
    		'taxonomy'                 => 'material'
    	));
    
        foreach ($cats as $cat) :
    
        $args = array(
        'posts_per_page' => -1, // max number of post per category
    	'post_type' => 'lesson',
        'tax_query' => array(
    							array(
    								'taxonomy' => 'material',
    								'field'    => 'term_id',
    								'terms'    => $category->term_id,
    								),
    						)	
    
        );
        $my_query = new WP_Query($args); 
    
            if ($my_query->have_posts()) :
            echo '<h3>'.$cat->name.'</h3>';
    
            while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <?php /*general loop output; for instance: */ ?>
            <a href="<?php the_permalink() ?>"><?php the_title(); ?></a>    <br />  
    
            <?php endwhile; ?>
    
            <?php else :
            echo 'No Posts for '.$cat->name;
            endif; 
    
        endforeach; ?>

    Hope that helps :slight_smile:

    Cheers, Sajid

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.