Home Forums General Issues Get conditional field by name


Get conditional field by name

  • Hi!

    I’m trying to get a field by it’s name using get_field_object(‘field-name’); but it’s not working.

    The field is conditional, it will appear in the editor only if another field of the same group matchs a certain condition. The data format is already set to “both”.
    I already tryed to use get_field(), get_sub_field() and get_sub_field_object() but it still doesn’t work, it gets nothing.

    It works when I get the field by it’s key, but I really need to get it by it’s name.

    Here is my code:

         $c = get_field_object('cidade-pr');
         if($c ) : 			
            echo '<select name=""><option value="" disabled selected>CIDADE</option>';
            foreach( $c['choices'] as $j => $l ) :						
               echo '<option value="' . $j . '">' . $l . '</option>';
            echo '</select>';
  • If the field does not have an entry in the DB for the post you are getting it for then you cannot get it by name. ACF uses the field key, when you supply the field name then ACF looks up the key by finding the field key reference for in the DB and uses the post ID and field name to get it. If there is no value saved in the DB then there is no field key reference and ACF is unable to locate the field.

  • Hi, John! Tahnk for your response, but I don’t know if I got it…

    The specific field that I uses to exemplify in the code above has posts that have values set to the field. And yet it doesen’t work…

    I’m trying to make state and city filter, by the way. So I want to list the cities of the selected state in another select box.

  • The value has to be set for the current post, it does not matter if other posts have a value.


    assumes that you want to get the field object associated with the global post.

    You should be getting the field that this field is conditional on, testing that field to decide whether or not to get this field.

    Or you need to use the field key

  • I’m new to wordpress coding and php, I’m building my solution on trial and error.
    Could you please explain me how should I get the field and test it?

  • As I said before, use the field key to get the field object. In your OP you said you already did this.

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

You must be logged in to reply to this topic.