I have a requirement to output taxonomies as a select list. This is acheived using the code below.
There are a couple of tweaks I would like, if possible:
1. Limit the output taxonomies by post type association. For example, only taxonomies used for products.
2. Set the output to the style of “taxonomy_slug : Taxonomy Name” so the select list looks nice on the frontend.
function pd_get_taxonomy_list( $field ) {
$field['choices'] = array();
$taxonomies = get_taxonomies();
foreach ( $taxonomies as $taxonomy ) {
$choices[$taxonomy] = $taxonomy;
}
$field['choices'] = $choices;
wp_reset_postdata();
return $field;
}
add_filter('acf/load_field/key=field_59da1b7571513', 'pd_get_taxonomy_list');
Hello @pixeldesignsuk
Am I correct in thinking that you are in fact looking for “Terms” of a “Taxonomy” and not actually the “Taxonomies” of a CPT?
If you are looking for “Taxonomy Terms” (as opposed to Taxonomies) which makes more sense if looking for the “slug”, then you could try this:
$field['choices'] = array();
$terms = get_terms( array(
'taxonomy' => 'custom_taxonomy_name',
'hide_empty' => false,
));
if ( $terms ) {
foreach ( $terms as $term ) {
$choices[$term->slug] = $term->slug . ' : ' . $term->name;
}
}
Reference: