• I am trying to get Advanced Custom Fields to work with custom taxonomies so I can add content to specific taxonomies in WP.

    I came across this documentation about adding custom location rules and added a new rule allowing the field group to be added to a category with get_categories() – and it worked perfectly.

    But as soon as I tried to use get_terms() with a custom taxonomy instead it stopped listing anything. My code looks like this:

    //add filter that lets you put a custom field group on a specific category in ACF
    add_filter('acf/location/rule_types', 'acf_location_rules_types');
    function acf_location_rules_types( $choices )
    {
        $choices['Forms']['custom_taxonomy'] = 'Custom Taxonomy';
    
        return $choices;
    }
    
    add_filter('acf/location/rule_values/category', 'acf_location_rules_values_category');
    function acf_location_rules_values_category( $choices )
    {
    
        $terms = get_terms('ranges',
          array(
              'hide_empty'=> FALSE)
          );
    
        if( $terms )
        {
            foreach( $terms as $term )
            {
                $choices[ $term->term_id ] = $term->name;
            }
        }
        return $choices;
    }
    
    add_filter('acf/location/rule_match/category', 'acf_location_rules_match_category', 10, 3);
    function acf_location_rules_match_category( $match, $rule, $options )
    {
        $current_cat = $_GET['tag_ID'];
        $selected_cat = (int) $rule['value'];
    
        if(isset($current_cat)){
          if($rule['operator'] == "==")
            {
              $match = ( $current_cat == $selected_cat );
            }
            elseif($rule['operator'] == "!=")
            {
              $match = ( $current_cat != $selected_cat );
            }
    
          return $match;
        }
    }

    My issue is with the foreach loop in the rule_values section – it should be listing the terms as it listed the categories.

    Does anyone have any idea why it might not be working?

    I have asked in the ACF forum but no luck.

  • The topic ‘get_terms() not working in the Admin’ is closed to new replies.