I'm running a wp_query on a page template in order to get posts from a custom post type "tour" filtered by a custom taxonomy "location". This taxonomy is hierachical and organised "land -> region -> spot". The current page that uses the template in question has the same taxonomy assigned by an ACF taxonomy field. This wp_query actually works fine:
<?php $args = array( 'post_type' => 'tour', 'location' => 'meran' ); $featured_tour = new WP_Query( $args ); ?>
<?php if ( $featured_tour->have_posts() ) : while ( $featured_tour->have_posts() ) : $featured_tour->the_post(); ?>
<article>
<h1><?php echo get_the_title(); ?></h1>
</article>
<?php endwhile; else : ?>
<p>No tour here! :( </p>
<?php endif; wp_reset_postdata(); ?>
I would now like to replace the name of the location by the term that's selected in the ACF "spot" field, something like: 'location' => 'function_that_inserts_current_ACF_term'. Is there any way to retrieve this? Thank you for any advice.
$term_field = get_field('my_taxonomy', $post->ID);
doesn't do the trick? – SBD$term_field
variable and declaration shouldn't be in yourargs
array. thelocation
array key needs to be a string value, which is the field you get from ACF. – SBD<?php $your_tax = get_field('tax', $page_id); //$page_id should be the page of 'Meran' ?> <?php $args = array( 'post_type' => 'tour', 'location' => $your_tax ); $featured_tour = new WP_Query( $args ); ?> <?php if ( $featured_tour->have_posts() ) : while ( $featured_tour->have_posts() ) : $featured_tour->the_post(); ?> <article> <h1><?php echo get_the_title(); ?></h1> </article> <?php endwhile; else : ?> <p>No tour here! :( </p> <?php endif; wp_reset_postdata(); ?>
You are now getting the ACF tax field if it exists in your current page. – SBD