Support

Account

Home Forums General Issues How to Narrow Categories Displayed in ACF? Reply To: How to Narrow Categories Displayed in ACF?

  • We’ll need more information to answer your question entirely, but hopefully this code can get you started.

    You mention that you need to narrow it down to one parent category, but then display a list of categories, so I’m not sure I’m understanding fully.

    The following example gets all top level categories from the default WordPress post type and populates an ACF select field called “test”. The following code should be inserted into your functions.php file.

    
    function acf_load_field_choices( $field ) {
    
      // Create an empty array
      $field['choices'] = array();
    
      // Get parent categories and order by name
      $categories = get_categories( 
        array(
          'orderby' => 'name',
          'parent'  => 0
        ) 
      );
    
      // Create a counter variable for the array
      $i = 0;
    
      foreach ( $categories as $category ) {
    
        // Add each category to the array
        $field['choices'][$i] = $category->name;
    
        // Increment the loop counter
        $i++;
    
      }
      
      // Return the result
      return $field;
    
    }
    
    add_filter('acf/load_field/name=test', 'acf_load_field_choices');