Support

Account

Forum Replies Created

  • 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 1 post (of 1 total)