Support

Account

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

Solving

How to Narrow Categories Displayed in ACF?

  • I need to take a long list of categories and narrow the choices down to one parent category. That filtered list would then be displayed in a Select ACF field available to staff. How can this be done? (I’m an ACF newbie, btw.)

  • 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');
    
  • Thanks for the example code.

    Let me provide a bit more details behind my question: I have nearly two-dozen categories, but there is only one top-level category and its three child categories from which I need to select.

    For instance, I have a top-level “Location” category and three sub-categories. Rather than list all categories in the ACF select box, I would like to narrow that to only the four categories.

    Again, thanks for the help.

  • No problem! In that case, try this.

    
    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',
    			'hide_empty' => 0,
    		) 
    	);
    
      	// Create a counter variable for the array
    	$i = 0;
    
    	foreach( $categories as $category ) {
    
    		// Get the top level categories
    		if ( $category->parent == 0 ) {
    
    			$field['choices'][$i] = $category->name;
    
    			$i++;
    
    			// Get subcategories within the parent category only
    			foreach ( $categories as $subcategory ) {
    
    				if ($subcategory->parent == $category->term_id) {
    
    					$field['choices'][$i] = $subcategory->name;
    					$i++;
    				}
    			}
    
    			$i++;
    
    		}
    	}
    
      	// Return the result
    	return $field;
    
    }
    
    add_filter('acf/load_field/name=test', 'acf_load_field_choices');
    
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘How to Narrow Categories Displayed in ACF?’ is closed to new replies.