Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Display custom post group by taxonomy current term and sub-term WordPress

  • SOLVED

I need display my posts group term and sub-terms. I am close but can make it work. My current code getting post from all terms but I need to get posts from only current term and its sub-term.

https://blog.dolil.com/manual_cat/registration-rules-2014/

User: roopokar Pass: 123456

taxonomy_manual-cat.php

<?php
/**
* The template for displaying taxonomy faq_cat
*/

get_header(); ?>

<div class="wrapper" id="manual-archive">

<div id="content" class="container-fluid">

<div class="row">

<div class="col-12 col-md-8">

<section id="breadcrumbs_top_content">
<?php
if ( function_exists('yoast_breadcrumb') ) {
yoast_breadcrumb( '<p id="breadcrumbs">','</p>' );
}
?>
</section>

<div class="content row">

<div id="full-width" class="col-12 content-area">

<main id="main" class="site-main" role="main">

<div class="gazette_archive_wrapper">

<?php // Output all Taxonomies names with their respective items
$current = $wp_query->get_queried_object();
$current_term_id = $current->term_id;
$current_name = $current->name;
$current_taxonomy = $current->taxonomy;
$current_parent = $current->parent;

//$terms = get_terms('manual_cat');

$terms = get_terms( array(
'taxonomy' => $current_taxonomy,
//'child_of' => $current_term_id,
//'hide_empty' => false,
//'parent' => 0,
) );

foreach( $terms as $term ):

?>
<section class="manual-header">

<h3><?php echo $term->name; // Print the term name ?></h3>

</section>

<?php
$posts = get_posts(array(
'post_type' => 'manual',
'tax_query' => array(
array(
'taxonomy' => $term->taxonomy,
'field' => 'slug',
'terms' => $term->slug,
//'operator ' => 'IN',
)
),
'posts_per_page' => -1,
'order' => 'ASC',
));

foreach($posts as $post): // begin cycle through posts of this taxonmy

setup_postdata($post); //set up post data for use in the loop (enables the_title(), etc without specifying a post ID)
?>
<section class="manual-entry">

<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>

</section>

<?php endforeach; ?>

<?php endforeach; ?>


</div><!-- .artcile_archive_wrapper-->

</main><!-- #main -->

</div><!-- #primary -->

</div><!-- .content -->

</div><!-- .row -->

</div><!-- .row -->

</div><!-- .container-fluid -->

</div><!-- .wrapper -->

<?php get_footer(); ?>


https://ibb.co/PmKs0Bh

Answers (2)

2021-04-19

Bob answers:

This will help you.

https://developer.wordpress.org/reference/functions/get_term_children/
get_term_children( int $term_id, string $taxonomy )
This will Merge all term children into a single array of their IDs.

Then pass that array into your post type tax_query with field "term_id"

2021-04-20

Francisco Javier Carazo Gil answers:

Instead of doing this, I would use the hook: pre_get_post.

I would change the default query to include there also the term children into the tax query that is being executed.