Scroll to top

How to get posts assigned to current taxonomy

Suppose you have created a custom post type with taxonomy. Now you want to create custom template for the taxonomy. To do this, you will have to create a file and upload it to the active theme folder with the name taxonomy-your_taxonomy_name.php

Now, you can use the following code to get the content in the taxonomy template. Please make sure to replace your taxonomy name in the code provided.

<div class="article-wrapper">


                    $terms = wp_get_post_terms( $post->ID, 'your-taxonomy'); 
                    $terms_ids = [];

                    foreach ( $terms as $term ) {
                        $terms_ids[] = $term->term_id;

                    $args = array(
                        'post_type' => 'your-post-type',
                        'tax_query' => array(
                            'relation' => 'AND',
                                'taxonomy' => 'your-taxonomy',
                                'field'    => 'term_id',
                                'terms'    => $terms_ids

                    $query = new WP_Query($args);
                    if ( $query->have_posts() ) {
                        while ( $query->have_posts() ) {

                     <div class="row">
                        <div class="col-sm-12 article-content">
                                <?php $query->the_post();?>
                                <div class="a-post-time">
                                    <span class="current-date"><?php the_time('j F, D') ?></span>
                                    <span class="current-time"><?php the_time('g:i a') ?></span>
                                <div class="article-title">
                                    <?php the_title(); ?> 
                                <div class="article-content">
                                    <div id="excerpt"><?php the_excerpt(); ?></div>

                                    <?php the_content(); ?>
                                <div class="article-tags">
                                    <?php echo get_the_term_list( get_the_ID(), 'your-taxonomy', '', ',' ); ?>

                    <?php } endwhile; wp_reset_query(); } ?>

Let me know if you have any questions in the comments section below.

Share Post:
Author avatar
Zahid Iqbal
I am a web designer and developer working full-time.

Post a comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.