Support

Account

Home Forums General Issues What's the best way to get the fields in a group in order, for display? Reply To: What's the best way to get the fields in a group in order, for display?

  • Well, after posting (isn’t it how this always works?) I figured out the brute force way to do it. It would be great if there were a function for this.

    
    // get the list of fields in order.
    	
    $args = array(
    	'post_type' => 'acf-field',
    	'post_parent' => $group_id, // field group 
    	'posts_per_page' => 100,
    	'orderby' => 'menu_order',
    	'order' => 'ASC'
    );
    	
    $all_fields = new WP_Query($args);
    	
    $fieldnames = array();
    
    if ($all_fields->have_posts()){
    	foreach($all_fields->posts as $f){
    		$fieldnames[] = $f->post_excerpt;
    	}
    }
    		
    // get the fields
    
    $fields = get_field_objects();
    
    if( $fields )
    {
    
    	foreach($fieldnames as $fn){
    		
    		if (isset($fields[$fn])){
    			$field =  $fields[$fn];
    			if ($field['value'] == '') continue;
    			/* do stuff */
    		}
    	}
    }