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;
}
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.