Support

Account

Home Forums Front-end Issues Custom taxonomy slug not ID (again, i know) Reply To: Custom taxonomy slug not ID (again, i know)

  • A taxonomy field by default allows multiple terms to be selected, which means that it will return either an array of IDs or an array of terms depending on what you have it set for. Unless you change it to a field type that only allows a single value.

    It is best to never assume what is being returned. It could be an array or a single value. The reason for this is that let’s say that you create a taxonomy field that allows multiple selections and you edit a few posts with this setup. If you then go in an change it to only allowing one selection the values in the posts you already edited will still contain arrays unless you go back and edit every post. Meanwhile any new posts you add will have just a single value.

    
    
    // if you are expecting a single value
    $term = get_field('your_taxonomy_field');
    if (is_array($term)) {
     $term = $terms[0];
    }
    echo $term->name;
    
    // if you are expecting an array
    $terms = get_field('your_taxonomy_field');
    if (!is_array($terms)) {
      $terms = array($terms);
    }
    foreach ($terms as $term) {
      echo $term->name;
    }
    

    If that doesn’t completely answer your question post the code you’re using.