Home › Forums › General Issues › Custom taxonomies are not fetched with acf
I have an incomprehensible problem, which is with custom taxonomies, I created a custom taxonomy named “teams” using the following code:
function add_post_taxonomy() {
$taxArray = array(
array(
"taxName" => 'Teams',
"taxNameEn" =>'teams'
),
);
foreach ($taxArray as $tax) {
$labels = array(
"name" => __( "", "" ),
"singular_name" => __( $tax['taxName'], "" ),
"menu_name" => __( $tax['taxName'], "" ),
);
$args = array(
"label" => __( $tax['taxName'], "" ),
"labels" => $labels,
"public" => true,
"hierarchical" => true,
"label" => $tax['taxName'],
"show_ui" => true,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"show_admin_column" => false,
"query_var" => true,
"rewrite" => array( 'slug' => $tax['taxNameEn'], 'with_front' => true, ),
"show_in_rest" => true,
"rest_base" => $tax['taxNameEn'],
"show_in_quick_edit" => true,
);
register_taxonomy( $tax['taxNameEn'], 'matches', $args );
}
}
add_action( 'init', 'add_post_taxonomy' );
And I created a function to get the custom terms as follows:
function get_terms_for_acf_select() {
$terms = get_terms(array('taxonomy' => 'teams', 'hide_empty' => false));
$options = array();
if (is_array($terms)) {
foreach ($terms as $term) {
if (is_object($term)) {
$options[$term->term_id] = $term->term_id;
}
}
}
return $options;
}
The problem is when I use it like this to get options, I don’t get anything
acf_add_local_field_group(array(
'key' => 'group_team_data',
'title' => 'team Data',
'fields' => array(
array(
'key' => 'field_team_info',
'label' => 'teams',
'name' => 'teams',
'type' => 'select',
'instructions' => 'Select the team',
'choices' => get_terms_for_acf_select(),
'return_format' => 'value', // Change 'label' to 'value'
),
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'matches',
),
),
),
'menu_order' => 0,
'position' => 'normal',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
));
}
Knowing that when I change teams to categories, it works normally
When is add_local_field_group called, before or after “init” when the taxonomy is added. I suspect that you are attempting to get the terms in the taxonomy before the taxonomy has been initialized.
function get_terms_for_acf_select() {
$terms = get_terms(array('taxonomy' => 'teams', 'hide_empty' => false));
if (is_wp_error($terms)) {
die ('WP Error Getting Terms');
}
$options = array();
if (is_array($terms)) {
foreach ($terms as $term) {
if (is_object($term)) {
$options[$term->term_id] = $term->term_id;
}
}
}
return $options;
}
You must be logged in to reply to this topic.
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.