I know this is an old question, but wanted to throw out my answer for others who may find this. There are a couple problems with your code:
According to the ACF docs for get_field_object(), they say that "In some circumstances it may be necessary to load a field by its key, such as when a value has not yet been saved." I have found that the field name does not work when trying to retrieve a field object outside of a loop (which is what you're doing here with trying to show all options within a field, not just those related to a specific post).
Another challenge is that the field key can differ across environments (local, staging, production, etc) unless you sync your databases. So here's a function that can help you find the field key by field name anytime you need to use it in a function such as get_field_object():
if (! function_exists('acf_field_from_name')) {
function acf_field_from_name($field, $group)
{
global $wpdb;
return $wpdb->get_var($wpdb->prepare("
SELECT post.post_name as field_name
FROM $wpdb->posts AS post
LEFT JOIN $wpdb->posts AS parent
ON post.post_parent = parent.id
WHERE post.post_excerpt = %s
AND post.post_type = 'acf-field'
AND parent.post_excerpt = %s
AND parent.post_type = 'acf-field-group'
", $field, $group));
}
}
Will return the field key from name and group, or null if none exists.
Usage:
acf_field_from_name('title', 'movie-fields'); // returns field_3333333333333
acf_field_from_name('title', 'book-fields'); // returns field_4444444444444
acf_field_from_name('plumbus', 'movie'); // returns null
See full answer by @Chris about why the group name is important in How to get Advanced Custom Fields field key from WordPress database?
Then your code would be:
$field_name = "team_category";
$group_name = "team_fields";
$field_key = acf_field_from_name($field_name, $group_name);
$team_category_field = get_field_object($field_key);
- The second problem is the options for select, checkbox, and radio fields are stored as an array inside an item within the field object array under the key
choices
. So to get those you need to use:
$team_category_choices = $team_category_field['choices'];
- The third problem is your foreach() function is incorrect. You need to pass the array and define how each of the array item keys (choice value saved to DB) and values (choice label shown in UI) in it will be referenced within the inner function, like so:
foreach($team_category_choices as $choice_value => $choice_label) {
echo '<option value="' . $choice_value . '">' . $choice_label . '</option>';
}