Home Forums General Issues gettings values from a select


gettings values from a select

  • Hello,
    I’m trying to get the values that are setup in a field of type select.
    The name of this field is “domaine_de_formation”.
    Its type is “select”.
    I added several values in it.
    I setup the return value as array.
    When I do this:

    get_field_object( 'domaine_de_formation' )

    The return value is always false, while I was expecting to get all the values I had setup.

    What is it wrong that I’m doing ?
    Could you please help me ?


  • get_field_object( 'domaine_de_formation' ) returns the field settings for the field. choices you added when creating the field will be in ['choices'].

    If you are trying to get the values that are set when adding/editing a post then you should use get_field() not get_field_object() or you need to look in ['value'] of the array returned by ‘get_field_object()`.

  • Exactly, I’m trying to get the values of the choices that I set when creating the field.
    You say they are in ['choices'] but I don’t understand…
    Actually, get_field_object( 'domaine_de_formation' ) returns false, and not any field settings.

    Could you please be more specific about this ['choices'] variable or function ?

    $field = get_field_object('domaine_de_formation');
    $choices = $field['choices'];

    if get_field_object(‘domaine_de_formation’) is returning false then ACF does not know what post you’re trying to get the fields from, or there are no values set for the current post. ACF cannot get the field object using the field name if values are not set for the current post or acf cannot determine what the current post is.

    I will need some more context on exactly how/where/when you are attempting to use this function.

  • That’s correct, I didn’t pass the value of the post because I don’t have any.
    Let me explain you the module :
    I created some custom posts which are “formation”.
    Each formation post type has several custom fields that are handled by ACF, and “type de formation” is one of them.
    When I created these custom fields, I added ome values, because these fields are select fields.
    So they have several possible choices, as you already understood.
    But the choices are independant from the is of the formation.

    In my module I’m going to create a kind of search engine that will contain 2 selects : one of them will be “type de formation”. That’s why I need to retrieve all the possible values that were set in ACF.
    So when a user chooses a “type de formation” in my search engine, then it will display all the “formations” of this type.

    Do you understand better ?

    So in my module i’m trying here to build the select.
    That’s why I wanted to get the object “domaine_de_formation”, and in this case, all the possible choices.

    Because let’s say tomorrow I add a new choice in ACF, I don’t want to have to edit my module, obviously.

    I don’t know if I’m clear… sorry for my english, I’m french 🙂
    Thanks for your help.


  • Finally I understood what was going on.
    I used the debugger and saw that internally the function tries to get an ID.
    And in my case, the ID was the page ID where the search engine is called.

    So what i did is : I changed the function that’s supposed to retrieve the values from the select. First thing I do is that I get one item from my custom post types and I get its ID. This way when I do the “get_field_object” call then it’s not false anymore.

    Thanks for your help.

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

You must be logged in to reply to this topic.