0

This feels like an easy question but I just cannot get it to work.

I have a basic custom post loop displaying posts based on a taxonomy of location and can then manually change the terms to show posts tagged with the same term. All works fine.

What I am trying to do is change the manual term selection for an ACF taxonomy field called 'case_study_location', so what ever term is selected there then shows the correct posts in my loop. I have tried many different things but cannot get it to work.

Here is my loop

<?php 

$loop = new WP_Query( array( 
                'post_type' => 'case_studies', 
                'posts_per_page' => 3, 
                'post_status' => 'publish',
                'tax_query' => array(
array(
'taxonomy' => 'location',
'field' => 'slug',
'terms' => 'location-two'
)
) ) ); ?>
                
            <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

                        <h3><?php the_title(); ?></h3>
                        <?php the_terms( $post->ID, 'location', 'categories: ', ' / ' ); ?>

            <?php endwhile; wp_reset_query(); ?>
2
  • you would need a mechanism to reload the page with the selected term ID/slug in the URL, so the_terms needs to be replaced. You would then need to modify the query to incorporate that information from the URL instead of hardcoding location-two. But I'm curious, why are you creating a brand new post loop when you could use the location taxonomy archives and template and have WP do it all for you out of the box?
    – Tom J Nowell
    Oct 19 at 15:38
  • Also wp_reset_query should only be used to cleanup after query_posts, aka it should never be seen or used in modern WordPress code. Burn the existence of this function from your memory with the fire of a thousand suns. wp_reset_postdata is the appropriate function ( to cleanup after the_post ). You are also missing the wrapping if statement, so you're cleaning up even when there is nothing to cleanup, which is a big problem if this query loop is nested
    – Tom J Nowell
    Oct 19 at 15:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.