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');
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.