Home › Forums › General Issues › Fix for Displaying Fields According to Field Order (using get_field_objects) › Reply To: Fix for Displaying Fields According to Field Order (using get_field_objects)
If anyone else is having issues with this I also came up with a solution:
Get all meta data for post/custom post type:
$meta_data = get_field_objects($post->ID);
Add a function to sort array by given key(in our case ‘menu_order’)
function array_sort($array, $on, $order=SORT_ASC){
$new_array = array();
$sortable_array = array();
if (count($array) > 0) {
foreach ($array as $k => $v) {
if (is_array($v)) {
foreach ($v as $k2 => $v2) {
if ($k2 == $on) {
$sortable_array[$k] = $v2;
}
}
} else {
$sortable_array[$k] = $v;
}
}
switch ($order) {
case SORT_ASC:
asort($sortable_array);
break;
case SORT_DESC:
arsort($sortable_array);
break;
}
foreach ($sortable_array as $k => $v) {
$new_array[$k] = $array[$k];
}
}
return $new_array;
}
Sort meta data ascending by calling function
$sorted_meta_data = array_sort($meta_data, 'menu_order', SORT_ASC);
Or sort meta data descending by calling function
$sorted_meta_data = array_sort($meta_data, 'menu_order', SORT_DESC);
Show all data. In my case I have a field called ‘product_image’ and I don’t want to show that field in my
<ul class="Product-data">
<?php
foreach ($sorted_meta_data as $meta) { ?>
<?php if( !empty($meta['value'])) : ?>
<?php if($meta == 'product_image') continue; ?>
<li class="Product-item u-block u-cf">
<span class="Product-label"><?php echo $meta['label']; ?></span>
<span class="Product-value"><?php echo $meta['value']; ?></span>
</li>
<?php endif; } ?>
</ul>
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 demoed ACF 6.1 Beta during the most recent session of ACF Chat Fridays, highlighting the new ability to regenerate and clear labels, setting the Admin Menu Parent as a slug, and more. Catch the video replay in our latest summary. https://t.co/rHEpPVas64 pic.twitter.com/hB1XKTexXi
— Advanced Custom Fields (@wp_acf) March 23, 2023
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.