$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>categories - How to use acf field value to order category?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

categories - How to use acf field value to order category?

matteradmin12PV0评论

I am trying to order category by acf field value. How can I achieve this using acf filed value?

    $categories = get_categories('taxonomy=toc_category');
    $sorted_cats = array();
    foreach($categories as $cat){
        $ordr = get_field('order', 'toc_category' . '_' . $cat->term_id);
        $sorted_cats[$ordr] = $cat;

        echo '<pre>';
        print_r($cat);
        echo $cat->name; // it not returns expexted output
    }
    krsort($sorted_cats);
    return $sorted_cats;

I am trying to order category by acf field value. How can I achieve this using acf filed value?

    $categories = get_categories('taxonomy=toc_category');
    $sorted_cats = array();
    foreach($categories as $cat){
        $ordr = get_field('order', 'toc_category' . '_' . $cat->term_id);
        $sorted_cats[$ordr] = $cat;

        echo '<pre>';
        print_r($cat);
        echo $cat->name; // it not returns expexted output
    }
    krsort($sorted_cats);
    return $sorted_cats;
Share Improve this question edited Mar 20, 2019 at 11:49 Aftab 1,3919 silver badges19 bronze badges asked Mar 20, 2019 at 7:36 AbheeAbhee 1035 bronze badges 5
  • Do you want to try to get acf filed of category? First you let me know how did you name acf fields? – Gufran Hasan Commented Mar 20, 2019 at 7:40
  • I want to display category name order by acf field value. I have added acf text filed to category. In which i had stored value like 1 for category 1,2 for category 2. – Abhee Commented Mar 20, 2019 at 7:41
  • Okay, My question is what is the name of order of acf 'toc_category' . '_' . $cat->term_id. – Gufran Hasan Commented Mar 20, 2019 at 8:17
  • As you mentioned that you have the category, category1, and category2. there should name for acf field order. – Gufran Hasan Commented Mar 20, 2019 at 8:18
  • Please this link hope it will helpful for you wordpress.stackexchange/a/151879/137328 – Gufran Hasan Commented Mar 20, 2019 at 8:20
Add a comment  | 

1 Answer 1

Reset to default 0

Use this code.

$categories = get_terms('toc_category');
$sorted_cats = array();
foreach($categories as $cat){
    $ordr = get_field('order', 'toc_category_'.$cat->term_id);
    $sorted_cats[$ordr] = $cat->name;
}
krsort($sorted_cats);
return $sorted_cats;
Post a comment

comment list (0)

  1. No comments so far