Display all custom post type posts from a custom taxonomy

Display Posts(Custom Post Type) from Specific Category (Custom Taxonomy)

You can get all the posts of custom post type from a category (custom taxonomy) by category slug. You can add below code in the page template or theme file where you want to add Posts.

   <!--?php
    $args = array('post_type' =------> 'custom_post_type',
        'tax_query' =&gt; array(
            array(
                'taxonomy' =&gt; 'custom_taxonomy',
                'field' =&gt; 'slug',
                'terms' =&gt; 'slug_of_custom_taxonomy',
            ),
        ),
     );
 
     $loop = new WP_Query($args);
     if($loop-&gt;have_posts()) {
 
        while($loop-&gt;have_posts()) : $loop-&gt;the_post();
            echo '
 
 
 
 
<div class="post"><a href="'.get_permalink().'">'.get_the_title().'</a>
';
            echo '
'.get_the_excerpt().'
 
';
            echo '<a href="'.get_permalink().'">View Details</a></div>
 
 
 
 
';
        endwhile;
     }else{
        echo '
 
 
 
 
<h2>No posts found in this Custom Taxonomy</h2>
 
 
 
 
';
     }
?&gt;

Display All custom taxonomy and there custom post types underneath those

You can list all posts of custom post type and arrange them by the custom taxonomy. You can add below code in the page template or theme file where you want to add Posts.

<!--?php
    $custom_taxonomys = get_terms('custom_taxonomy');
 
    foreach($custom_taxonomys as $custom_taxonomy) {
        $args = array('post_type' =------> 'custom_post_type',
            'tax_query' =&gt; array(
                array(
                    'taxonomy' =&gt; 'custom_taxonomy',
                    'field' =&gt; 'slug',
                    'terms' =&gt; $custom_taxonomy-&gt;slug,
                ),
            ),
         );
 
         $loop = new WP_Query($args);
         if($loop-&gt;have_posts()) {
 
            echo '
 
 
 
 
<h2>'.$custom_taxonomy-&gt;name.'</h2>
 
 
 
 
';
 
            while($loop-&gt;have_posts()) : $loop-&gt;the_post();
                echo '
 
 
 
 
<div class="post"><a href="'.get_permalink().'">'.get_the_title().'</a>
';
                echo '
'.get_the_excerpt().'
 
';
                echo '<a href="'.get_permalink().'">View Details</a></div>
 
 
 
 
';
            endwhile;
 
         }else{
            echo '
 
 
 
 
<h2>'.$custom_taxonomy-&gt;name.'</h2>
 
 
 
 
';
            echo '
 
 
 
 
<h2>No posts found in this Custom Taxonomy</h2>
 
 
 
 
';
         }
    }
?&gt;

Leave a Reply

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