Support

Account

Home Forums ACF PRO Trying to populate select field with all tags on site

Helping

Trying to populate select field with all tags on site

  • I’m trying to populate a select field with all the tags on my posts. However $choices = get_tags(); doesn’t seem to work – but when I use a manual array e.g. $choices = array(“one”, “two”, “three”); this works fine. What’s going on? Thank you 🙂

    function acf_load_tag_field_choices( $field ) {

    // reset choices
    $field[‘choices’] = array();

    //gets all tags in the array choices

    // $choices = array(“one”, “two”, “three”); this works
    $choices = get_tags(); // not working

    // loop through array and add to field ‘choices’
    if( is_array($choices) ) {

    foreach( $choices as $choice ) {

    $field[‘choices’][ $choice ] = $choice;

    }

    }

    // return the field
    return $field;

    }

    add_filter(‘acf/load_field/name=tag1’, ‘acf_load_tag_field_choices’);

  • get_tags() returns an array of term objects

    
    $tags = get_tags();
    foreach ($tags as $tag) {
      $choices[$tag->term_id] = $tag->name; 
    }
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.