Home › Forums › Front-end Issues › Get User Sub-Field Select Options
I am attempting to get a list of all of the options available for a particular select field. That select field is within a repeater field, and is associated with user profiles, rather than posts.
I have tried using get_field_objects()
to get the full list of fields, but, unless I specify a User ID that actually has data associated with my custom fields, the get_field_objects()
function returns false
.
So, if I try get_field_objects()
all by itself, I get false
.
If I try get_field_objects( 'user_1' )
, I also get false
, because the user with an ID of 1 hasn’t added any of the ACF field information to their profile.
I am finding that I have to know ahead of time which user(s) has added extra information to their profile before I can even get the list of fields.
Then, of course, I have to loop through the sub-fields until I find the one I’m looking for, and return its choices.
Is there any easier way to just get all field/sub-field objects that are assigned to the User Profile form; or do I have to know a specific user ID in order to get them? Thanks.
Are you trying to get the choice of the select field?
Or all of the choices that have been selected?
I’m so sorry for not replying sooner; somehow I overlooked your response.
Are you trying to get the choice of the select field?
Or all of the choices that have been selected?
Neither, I am trying to get all of the available options for a select element.
For instance, let’s say I have the following select element:
<select name="my-select">
<option value="first-option">First Option</option>
<option value="second-option">Second Option</option>
<option value="third-option">Third Option</option>
<option value="fourth-option">Fourth Option</option>
<option value="fifth-option">Fifth Option</option>
</select>
I want to be able to query ACF somehow to get an array that might look something like:
array(
'first-option' => 'First Option',
'second-option' => 'Second Option',
'third-option' => 'Third Option',
'fourth-option' => 'Fourth Option',
'fifth-option' => 'Fifth Option',
);
Thanks.
You’re still loosing me.
We’re talking about an ACF select field. When you create the field you set the choices.
get_field('field_name')
will get what has been selected for a specific post.
get_field_object('field_name')
will get the field object, including the the choices you have defined. However, this call (using the field name) will only work if this field has been updated and has a value assigned to it.
if you want to get a field object even when no value has previously been saved to it then you need to use the field key. get_field('field_1234567')
where ‘field_1234567’ represents the field key of the select field.
When you get the field object $field = get_field_object($selector)
$field will contain all the setting for the field and the choices that you’ve set for the field will be in $field['choices']
. This will be an array containing value=>label
pairs.
The topic ‘Get User Sub-Field Select Options’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.