Support

Account

Home Forums General Issues Sorting categories list by custom field Reply To: Sorting categories list by custom field

  • Hi, perhaps this can help:
    you could get your categories into an array, then create another array where you use the custom fields as keys and the previous array items as values.
    then sort that second array and iterate it do display whatever you wanted to display.
    here’s a function that does that, customize the $args to meet your needs of course.

    function get_article_categories_by_disp_order(){
    $args = array(
    	'type'                     => 'post',
    	'child_of'                 => 2,
    	'parent'                   => '',
    	'orderby'                  => 'name',
    	'order'                    => 'ASC',
    	'hide_empty'               => 0,
    	'hierarchical'             => 1,
    	'exclude'                  => '',
    	'include'                  => '',
    	'number'                   => '',
    	'taxonomy'                 => 'category',
    	'pad_counts'               => false 
    
    );
    	$categories = get_categories( $args );
    	$sorted_cats = array();
    	foreach($categories as $cat){
    		$ordr = get_field('display_order', 'category_'.$cat>term_id);
    		$sorted_cats[$ordr] = $cat;
    	}
    	ksort($sorted_cats);
    	return $sorted_cats;
    }