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


Custom taxonomy slug not ID (again, i know)

  • I swear i’ve googled the last few hours for this, searched this forum, tried a bunch of code snippets, but no luck so far.
    I’ve created a custom taxonomy, linked with my custom post but i coudn’t manage to echo the name of the category but only the id.
    If i switch from ID term to term object i get the nasty Object of class WP_Term could not be converted to string error.

    Any kind soul willing to help me?

    P.S.:The taxonomy (created with Custom Post Type UI) has these properties:

    name: tecnica
    label: tecniche
    singular_label: tecnica
    public: true
    hierarchical: true
    show_ui: true
    query_var: true
    rewrite: true
    rewrite_withfront: true
    rewrite_hierarchical: true
    show_admin_column: true
    show_in_rest: true
    show_in_quick_edit: true
  • 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.

  • Wow!
    Thanks for the super quick reply!

    Unfortunately it didn’t do the magic.
    Actually, pretty weird, your code (with the right field name “tecnica”) doesn’t output anything at all!

    My code is:

    <?php if( get_field('tecnica') ): ?>
    <?php $field_name = "text_field"; $field = get_field_object(tecnica); echo $field['label']; ?>:<?php the_field('tecnica'); ?>
    <?php endif; ?>

    (i need the label too, for possible future translations)

    I’ve set it to use radio button, because i need just one category (the tecnique used for a painting,it’s for a painter catalogue).

    I’ve tried to set as an html select, or a multiple category,and i get the array, but always with the id.

    The code you suggested with the correct field ‘tecnica’ is

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

    I’m working on Xaamp on my old laptop, PHP Version is 5.6.14…

  • Can you attach a screen shot of you’re field settings

  • The image did not post.

    looking at the code you posted, no matter what you’re returning you can’t use
    this will attempt to echo the field contents, since the field contents will be one of 4 things

    • A Term ID
    • A Term Object
    • An Array of Term IDs
    • An Array of Term Objects

    echoing the value will not produce the results you are expecting.

    Please try to repost the field settings or tell me what they are. Is the field you showing, a checkbox, a multi select, a radio or a single select field?

  • Now the field is a Term ID, and it works, showing the numeric ID of my taxonomy.
    And is a radio field.

    Meanwhile i’ve uploaded my work in progress in a live server, with no improvements…

    I’ve found this code and it seems to work!

    <?php   // Get terms for post
     $terms = get_the_terms( $post->ID , 'tecnica' );
     // Loop over each item since it's an array
     if ( $terms != null ){
     foreach( $terms as $term ) {
     // Print the name method from $term which is an OBJECT
     print $term->name ;
     // Get rid of the other data stored in the object, since it's not needed
    } } ?>

    from here, where is also suggested to use wp_get_object_terms( $object_ids, $taxonomies, $args )

    I’ve tried on new and old posts, changing the category changes correctly in the front end.

    Can we marked this [resolved]?
    Thanks again!

  • The code you posted is not getting the terms from the ACF field but with standard WP functions. You can do it that way.

    If you’re using a radio field then it’s returning a single field and if it’s set to return the ID then….

    $taxonomy = 'your-taxonomy'; // change this to your taxonomy
    $term_id = get_field('tecnica');
    $term = get_term_by('id', $term_id, $taxonomy);
    echo $term->name;

    It would be easier to return the term object, then you can just do

    $term = get_term('tecnica');
    echo $term->name;
  • yes!
    I switched to term object, and it worked as expected, on new posts!

    Grazie mille (thanks a lot!)

  • I am trying this and getting:


    any ideas? i have tried it every which way.

  • This means that get_term_by() is probably returning false. This means that either the taxonomy or the term does not exist.

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

The topic ‘Custom taxonomy slug not ID (again, i know)’ is closed to new replies.