Support

Account

Home Forums Front-end Issues Retrieve taxonomy based on ACF field.

Solved

Retrieve taxonomy based on ACF field.

  • I have a custom taxonomy called ‘Shows’.

    I’m using the php below to show items from that taxonomy that also have the ACF field ‘podcast_country’ (type = text) populated with ‘Australia’.

    This works perfectly in that case.

    I also have an ACF field called ‘podcast_topic’ (type = taxonomy, appearance = multi-select, return value = Term Object). I would like to use a similar code block to retrieve items with the topic ‘educational’.

    I cannot for the life of me work out how to do it. Other text fields work fine when checking against a single word, but when it comes to the taxonomy field I just get an empty screen – so clearly I’m missing something around that field type – but hours of searching and experimentation has gotten me nowhere.

    What changes do I need to make to this to make it work with an ACF taxonomy field?

    <?php
    $madein = get_terms('shows',array('hide_empty' => 0));
            echo '<div class="row mb-5"><div class="col-12"><h3>Made in Australia</h3></div>';
            foreach($madein as $term)
            {
                if( get_field('podcast_country', 'shows_'.$term->term_id) != 'Australia' )
                    continue;
              echo '<div class="col-6 col-md-3 col-lg-3 col-xl-2 p-2"><a href="' . get_term_link( $term ) . '">'; 
              echo wp_get_attachment_image( get_field('podcast_category_thumb', $term), 'thumbnail', false,  array('class' => 'img-responsive rounded','style' => 'width:100%;height: auto; aspect-ratio: 1 / 1') );
              echo '</a></div>';
            }
    echo '</div>';
    ?>
  • If you are trying to show posts that have both a spcific “podcast_country” and a specific “podcast_topic” then you need to query posts instead of trying to list terms.

  • Thanks, but I’m trying to show the ‘Shows’ taxonomy items that have ‘podcast_topic’ applied to them. Not the posts.

  • So I’m a little confused. You have a taxonomy field on a taxonomy. So when you’re editing a term in “podcast_country” you select a “podcast_topic”?

  • Kind of, the taxonomies are:

    Shows
    Country
    Topics

    Under shows I can assign a topic, and the country (amongst other fields) via ACF. The code I added at the top allows me to display a list of Show Taxonomy items based on the country field (podcast_country) – it’s a single item so I used a text field and the query was quite easy to create.

    What I’m trying to do is list the Shows based on the ‘podcast_topic’ field, but because that’s a taxonomy it outputs as an array and I can’t figure out how to get it to work.

    shows taxonomy

  • Sorry, but I have no idea from your description what you are working or how these things relate to each other or to where you are entering the information.

  • No worries, thanks for taking the time to look. It turned out my main issue was converting the array to a string, in case this is ever useful to anyone else… this worked

    <?php
      echo '<div class="row mb-5"><div class="col-12"><h3>History</h3></div>';
      foreach($madein as $term)
        {
        $a = get_field('podcast_topic', 'shows_'.$term->term_id); // get the field array
        $topicforuse = json_encode($a); // use json to convert to a string
        if (strpos($topicforuse, 'history') !== false) { //check the string for a keyword
          echo '<div class="col-6 col-md-3 col-xl-2 p-2"><a href="' . get_term_link( $term ) . '">'; 
          echo wp_get_attachment_image( get_field('podcast_category_thumb', $term), 'thumbnail', false,  array('class' => 'img-responsive rounded','style' => 'width:100%;height: auto; aspect-ratio: 1 / 1') );
          echo '</a></div>';
        }}
      echo '</div>';
    ?>
Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.