Support

Account

Home Forums General Issues Unable to display tax_query

Solving

Unable to display tax_query

  • I’m new to ACF and having trouble displaying posts by taxonomy. I’ve been working on this for two days with no success. Any help would be greatly appreciated.

    I’ve created a field group for real estate properties that has several text fields set up to contain general information: name, address, description, etc. Within it I’ve also created a taxonomy field for “Property Type”. It is set to display by category and return a Term ID. There are two categories: “land” and ‘retail”.

    All properties display as they should should on an archive page, but, additionally, I need to be able to display the posts from the categories ‘land’ and ‘retail’ on their own separate pages.

    I’ve created a category-retail.php page which I have confirmed is working, but I cannot get it to display the posts from just one property type. The content area displays blank.

    Here’s the code:

    <?php $args = array(
    ‘post_type’ => ‘properties’,
    ‘tax_query’ => array(
    array(
    ‘posts_per_page’ => -1,
    ‘taxonomy’ => ‘property_type’,
    ‘field’ => ‘slug’,
    ‘terms’ => ‘retail’
    ),
    ),
    );

    $query = new WP_Query( $args );
    while ( $query->have_posts() ) : $query->the_post(); ?>

    <?php the_title();?>
    <h2 class=”prop-address”><?php the_field(‘property_address’); ?></h2>
    <p class=”prop-description”><?php the_field(‘property_description’); ?></p>

    <?php endwhile; ?>

    Again, any help would be appreciated. Thanks.

  • try adding a $term to your custom field & use get_field instead of the_field

    $term = get_queried_object();
    $property_adress = get_field('property_adress',$term);
    echo $property_adress;
  • Hey HimSelf, thanks for your help on this.

    Sorry, but I don’t know how to add a $term to the custom field. Some of the articles I’ve viewed show a Taxonomy Term option in the Field Type dropdown, but my version of ACF does not have that option (see attached screencapture).

    I did notice a ‘Create Terms’ toggle at the bottom of that screenshot, but cannot find documentation on how to use it. Any advice?

    Thank you again for your reply. I really appreciate your help.

  • See the code i added at the end of my previous reply.

  • Yes, I saw that. I must not have used it correctly. If you don’t mind looking at it the current code is below> Thanks again.

    <?php $args = array(
    ‘post_type’ => ‘properties’,
    ‘tax_query’ => array(
    array(
    ‘posts_per_page’ => -1,
    ‘taxonomy’ => ‘property_type’,
    ‘field’ => ‘slug’,
    ‘terms’ => ‘retail’
    ),
    ),
    );

    $query = new WP_Query( $args );
    while ( $query->have_posts() ) : $query->the_post(); ?>

    <?php
    $term = get_queried_object();
    $property_address = get_field(‘property_address’,$term);
    echo $property_address;
    ?>

    <?php the_title();?>
    <h2 class=”prop-address”><?php the_field(‘property_address’); ?></h2>
    <p class=”prop-description”><?php the_field(‘property_description’); ?></p>

    <?php endwhile; ?>

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Unable to display tax_query’ is closed to new replies.