WordPress custom taxonomy query to list custom post types tagged with a taxonomy term from a custom vocabulary.

WordPress custom taxonomy query to get posts

The following snippet lists all posts tagged with a certain term from a custom vocabulary.

In this example I have a custom post type pods_cpt_people and a custom vocabulary pods_tax_people_type.  In this vocabulary i have a term with the term slug the-slug.

The snippet will list all posts of this custom post type tagged with the-slug taxonomy term.

<?php 
    // Query Arguments
    $args = array(
        'post_type' => 'pods_cpt_people', // the post type
        'tax_query' => array(
            array(
                'taxonomy' => 'pods_tax_people_type', // the custom vocabulary
                'field'    => 'slug',                 
                'terms'    => array('the-slug'),      // provide the term slugs
            ),
        ),
    );

    // The query
    $the_query = new WP_Query( $args );

    // The Loop
    if ( $the_query->have_posts() ) {
        echo '<h2> List of posts tagged with this tag </h2>';

        echo '<ul>';
        $html_list_items = '';
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
            $html_list_items .= '<li>';
            $html_list_items .= '<a href="' . get_permalink() . '">';
            $html_list_items .= get_the_title();
            $html_list_items .= '</a>';
            $html_list_items .= '</li>';
        }
        echo $html_list_items;
        echo '</ul>';

    } else {
        // no posts found
    }

    wp_reset_postdata();

    ?>
 ?>

Please ♥ like the page if you found this useful!