Not sure how well I can explain this but I will try. I have two Custom Post Types. One is "Locations" and one is "Members." I need to show only the members that belong to the location. "Locations" has an Advanced Custom Field of 'city' that would match "Members" taxonomy of 'location.' So, I want to know how I can only show posts if a term matches a field. Here is my WP_Query for "Members" that displays all "Members" separated by 'location' terms:

<?php $loop = new WP_Query( array( 'post_type' => 'member', 'posts_per_page' => -1 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); $do_not_duplicate = $post->ID; ?>
    <?php the_post_thumbnail('thumbnail'); ?>
    <div class="caption">
        <h4><?php the_title(); ?></h4>
                $term_names = wp_get_post_terms($post->ID, 'location', array ( 'fields' => 'names' ) );
                echo wp_sprintf_l( '%l', $term_names );
<?php endwhile; wp_reset_query(); wp_reset_postdata(); ?>

I also have this:

<?php $city = get_field('city'); ?>

So, what I'm asking is how do get only the posts with taxonomy 'location' term that matches '$city' to display?

1 Answer 1


Should be easy enough. First, get the City.

<?php $city = get_field('city'); ?>

Then in your WP_Query, insert the taxonomy parameter:

<?php $loop = new WP_Query( array( 'post_type' => 'member', 'posts_per_page' => -1, 'location' => $city ) ); ?>

Et Vilola: you only get the Members that have the taxonomy $city.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.