Home › Forums › General Issues › Order of fields in ACF get_fields();
hi.
So the way I’m using ACF is I have lots of fields in my post and then I print them all out from the get_fields() object using 7 nested loops which check for types and do styling and other magic.
It works great. There is only 1 problem.
Lets say I have fields:
field1
field2
field3
Then I want to duplicate field2, call it NewField and position between 2 and 3. I drag and drop in the fields editor so it looks like:
field1
field2
NewField
field3
great. I go to post editor and the order is fine. But later when i look at the frontend the order of the fields printed out is:
field1
field2
field3
NewField
The reason is, the array get_field() is populated in order of the fields being created and it doesn’t change when I reorder the fields in the backend.
Is there a possibility to reorder the array rows to resemble the order in the backend editor?
thanks a million!
solved it using something like this
$fields = get_field_objects();
if( $fields ):
$fields = array_orderby($fields, 'menu_order', SORT_ASC);
foreach( $fields as $name => $value ):
[...]
function to order multidensional array:
function array_orderby()
{
$args = func_get_args();
$data = array_shift($args);
foreach ($args as $n => $field) {
if (is_string($field)) {
$tmp = array();
foreach ($data as $key => $row)
$tmp[$key] = $row[$field];
$args[$n] = $tmp;
}
}
$args[] = &$data;
call_user_func_array('array_multisort', $args);
return array_pop($args);
}
Hi vandelay,
the problem you describe is excactly what I stumbled on as well. While looking for a solution I came across your post.
I tried to adapt your code from above, but I think I donĀ“t get it.
Here is code I am using right now, where the sorting order of the field is still wrong:
$lifestyle_data_fields = get_fields($post->ID);
// Here I need to get the fields and order them like they are ordered in the WP-Backend for a lifestyle_data post (or like they are ordered in the field-group, which is the same)
foreach ($lifestyle_data_fields as $name => $values) {
if ($values) {
foreach ($values as $value) {
...
Now I tried to adapt your code to match mine:
$lifestyle_data_fields = get_fields($post->ID);
function array_orderby()
{
$args = func_get_args();
$data = array_shift($args);
foreach ($args as $n => $field) {
if (is_string($field)) {
$tmp = array();
foreach ($data as $key => $row)
$tmp[$key] = $row[$field];
$args[$n] = $tmp;
}
}
$args[] = &$data;
call_user_func_array('array_multisort', $args);
return array_pop($args);
}
$lifestyle_data_fields = array_orderby( $lifestyle_data_fields, 'menu_order', SORT_ASC);
foreach ($lifestyle_data_fields as $name => $values) {
if ($values) {
foreach ($values as $value) {
...
But I get an error message “Warning: Illegal string offset ‘menu_order’ ” and the sorting order is not changed.
Can you or somebody else help me to get this sorted?
Thanks in advance for any input,
Hendrik
The topic ‘Order of fields in ACF get_fields();’ 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.