I’m now using Advanced Custom Fields Plugin in wordpress.
And now I defined some Choices, and they have Choices set as below:
Field Name: time_availability
Field Type: Select
low : Once or twice
high : Several times
So, I want to enumerate these settings in the page code, like rendering:
<ul>
<li><input type="radio" name="low" /> Once or twice</li>
<li><input type="radio" name="high" /> Several times</li>
</ul>
How can I get the choices?
Update:
Seemed if calling the get_field_object($field_name)
may got the entire field settings, including the choices.
But when I call the function with the field name, the below is returned:
array(18) {
["key"]=>
string(33) "field_time_availability"
["label"]=>
string(0) ""
["name"]=>
string(27) "time_availability"
["_name"]=>
string(27) "time_availability"
["type"]=>
string(4) "text"
["order_no"]=>
int(1)
["instructions"]=>
string(0) ""
["required"]=>
int(0)
["id"]=>
string(37) "acf-field-time_availability"
["class"]=>
string(4) "text"
["conditional_logic"]=>
array(3) {
["status"]=>
int(0)
["allorany"]=>
string(3) "all"
["rules"]=>
int(0)
}
["default_value"]=>
string(0) ""
["formatting"]=>
string(4) "html"
["maxlength"]=>
string(0) ""
["placeholder"]=>
string(0) ""
["prepend"]=>
string(0) ""
["append"]=>
string(0) ""
["value"]=>
bool(false)
}
Not the expecting result.
While if I call that function with the raw field name:
get_field_object('field_55df081515e81');
It goes right!
What’s the matter? What’s the difference and how can I get it right using the field_name?
Finally, after all, I found a reliable solution to get around this question:
You can first see the
api_acf_load_field
function in the source code:We can just iterate through all registered fields to pick the correct one.
But the
$GLOBALS['acf_register_field_group']
is there only when we callregister_field_group
explicitly (Case 2 below). Or in the normal case (Case 1 below), we can’t got it in this way.So see the different cases:
Case 1: When the field groups are defined in the admin panel (in the database):
In this case, the field groups are store as post which post type is
acf
, and the field information are stored aspost_meta
of that field group.So, we grab all the fields registered (from database), and pick the correct one(s).
Case 2: When
register_field_group
explicitly, like using the export php scripts in thefunctions.php
:Then we finally got a always work function:
Not sure why
get_field_object
always return false, and the answer I voted before not working any more, so I have to work out a new solution, I guess maybe ACF updated how data store, custom field now store inwp_post
table, post type isacf-field
.On ACFs site they state:
“The API will return the selected value. If you select the multiple option for this field, the API will return an array of values.”
So try the following:
If you have enabled the selection of multiple values, you should use checkboxes instead of radios.
To access the available choices, you have to get the field object.
To get the field object from a user, use this notation: