0
votes

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.

1
$term_field = get_field('my_taxonomy', $post->ID); doesn't do the trick?SBD
it's giving me a syntax error: Parse error: syntax error, unexpected T_STRING, expecting ')' in /Applications/AMPPS/www/.../wp-content/themes/.../template-leaflet-archive.php on line 94. I was trying like so: <?php $args = array( 'post_type' => 'tour', 'location' => '$term_field = get_field('spot', $post->ID);' ); $featured_tour = new WP_Query( $args ); ?>Ralf Glaser
The $term_field variable and declaration shouldn't be in your args array. the location array key needs to be a string value, which is the field you get from ACF.SBD
I'm not shure if I got you right ... could you please give me a hint of what code to use? Thank youRalf Glaser
<?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

1 Answers

1
votes
    <?php $your_tax = get_field('tax', $page_id); //$page_id should be the page of 'Meran'
$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.