Support

Account

Home Forums General Issues Sorting categories list by custom field

Solving

Sorting categories list by custom field

  • Couldn’t find a solution by searching the forms.

    I am trying to sort a categories list by my custom field, like so:

    wp_list_categories(‘child_of=57&meta_key=sort_name&orderby=meta_value_num&order=asc&hide_empty=0&use_desc_for_title=1&hierarchical=1&title_li=’);

    It isn’t working though…Any ideas?

  • Hi @apinrise

    Unfortunately, taxonomies do not yet have a termdata table, and can’t be sorted / ordered.

    Please read the WP docs on the above function to learn what is and isn’t available.

    Perhaps you can look into a WP plugin that offers taxonomy ordering. I have used a few in the past with successful results.

    Thanks
    E

  • 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;
    }
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Sorting categories list by custom field’ is closed to new replies.