admin管理员组

文章数量:1435859

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;

本文标签: categoriesHow to use acf field value to order category