Get Posts by Custom Post Type ,Taxonomy, and Term

Ok, so i have a Custom Post Type called “Services”. This custom post type has a taxonomy called “Areas” and there’s 5 terms in that taxonomy.

Let’s say I have 10 posts on “Services” and there’s 5 posts in the term “Painting” and 5 more on the term “Photography”.

I need to be able to query posts from “Services” but instead of showing those 10 posts, only show 5 associated to “Painting”.

At the moment i’m able to query by taxonomy and terms, but that will show all of the posts from “services” with no filter by term.

Basically query post by post_type from the term I choose.

Any help would be awesome. Thanks.

<ul id="service-list">
<?php 
        $args = array('tax_query' => array( array('taxonomy' => 'areas', 'field' => 'slug','terms' => 'painting')));

        $the_query = new WP_Query( $args );

        if($the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();

        ?>

    <li class="service">
        <h2><?php the_title(); ?></h2>
        <?php the_content(); ?>
    </li><!-- /.service -->

<?php endwhile; else: ?>

    <p>Nothing Here.</p>

<?php endif; wp_reset_postdata(); ?>

</ul><!-- #service-list -->

So if I could just specify on the $args from which post type to get the posts from this would be solved.

1

This is the answer to the question 🙂

<?php 

$args = array(
    'post_type'=> 'services',
    'areas'    => 'painting',
    'order'    => 'ASC'
);              

$the_query = new WP_Query( $args );
if($the_query->have_posts() ) : 
    while ( $the_query->have_posts() ) : 
       $the_query->the_post(); 
       // content goes here
    endwhile; 
    wp_reset_postdata(); 
else: 
endif;

?>

Leave a Comment