Support

Account

Home Forums General Issues Fix for Displaying Fields According to Field Order (using get_field_objects)

Solving

Fix for Displaying Fields According to Field Order (using get_field_objects)

  • The Problem
    This plugin currently allows for you to update the field order via the drag & drop reordering in the site admin, but that then doesn’t have any effect on the order of the fields being displayed which have content entered in prior to the reordering. This is the case even when using the built-in get_fields() and get_field_objects() functions as it appears they do not retrieve the fields by the field order (order_no) that’s specified.

    The Fix/Workaround
    We need to use get_field_objects() to retrieve the full field data, sort that field array by the order_no value, and then use that to display the fields.

    Here’s an example which gets the fields for a post and displays any fields that have a value in the field order that’s set in the site admin:

    
    $fields = get_field_objects($post->ID);
    if( !empty($fields) ){
    	uasort($fields,'compareOrderNo'); // compareOrderNo is located in functions.php
    	echo '<dl>';
    	foreach( $fields as $field ){
    		if($field['value'] != ''){
    			if($field['label'] != ''){
    				echo '<dt class="label">'.$field['label'].'</dt>';
    			}
    			if($field['value'] != ''){
    				echo '<dd class="value">'.$field['value'].'</dd>';
    			}
    		}
    	}
    	echo '</dl>';
    }
    

    This code uses a custom compareOrderNo() function which I’ve added to the site’s functions.php file:

    
    function compareOrderNo($elem1, $elem2) {
    	return strcmp($elem1['order_no'], $elem2['order_no']);
    }
    

    I’d love to see this behavior (display fields by the field order that’s currently set) officially adopted by the get_fields() and/or get_field_objects() functions. I’d be happy if it was an option; if not the default behavior.

    I’ve gone ahead and also posted it on the WordPress.org forums so others can find this: https://wordpress.org/support/topic/fix-for-displaying-fields-according-to-field-order-using-get_field_objects/

  • The best way to get the developer’s attention on this would be to open a new support ticket https://support.advancedcustomfields.com/new-ticket/.

    This is a good solution that others may find useful. This question comes up once in a while here.

    I personally do not use get_fields because I’ve never seen a time where I can generically show all the field data associated with a post. Almost always there are fields that require more formatting or special handling. Basically, the developers view of using this function can be found on the documentation for get_fields https://www.advancedcustomfields.com/resources/get_fields/

    You should only use this function when you do not know the fields which which will be appearing on a template or if you have a large amount of fields which you would prefer not to code independently.

  • That’s the thing though… I don’t know the fields being returned on the page. I want it to display all fields for that post; which may change over time. Also, I also want it to display these in the correct order; which may also change over time.

    Seems like a use case which could/should be accounted for.

    I’ll be sure to submit the ticket, thanks!

  • I was looking through the code of ACF to see if there was some performance reason for why it does not sort the values into display order. I can’t find any reason since ACF calls get_field_object. The only extra overhead is the shorting process. It could be that it’s just something that was never thought of, or there could be some other reason that I’m not aware of. In either case, a support ticket would definitely be your best way to get the developer’s input on it. For all I know this is already on a “to do” list and other things have priority.

  • @hube2 (John),

    Elliot (per the support ticket) doesn’t seem to think it’d affect performance much either, and has said it’s on the to-do list.

    Thanks!

  • 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

    • so I ignore it in my loop.
      <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>
Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Fix for Displaying Fields According to Field Order (using get_field_objects)’ is closed to new replies.