I was trying forever to get a good way to allow a user to select from multiple different taxonomies in one ACF drop down. I eventually realized that is just not possible.
Instead, what we need to do is use a select field and populate it dynamically.
To do this, simply create a select field in your ACF. I ended up doing this inside of a repeater, like so:
Now we just need to populate this select field to simulate our taxonomy field. Here is the code for that:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
/** * Populate the link selection of the quick links for use on the dashboard */ add_filter('acf/load_field/key=field_5776bde6e0201', 'jb_populate_select_w_tax'); function jb_populate_select_w_tax($field) { static $list = null; // reset choices $field['choices'] = array(); // return cached list if($list !== null) { return $list; } // initialize choice list $list = array(); // find the categories we want to add $terms = get_terms( array( 'taxonomy' => array('tax-slug-1','tax-slug-2'), 'hide_empty' => false, 'parent' => 0 //we are only getting the top level taxonomies here ) ); if(is_array($terms) && (count($terms) > 0)) { foreach($terms as $term) { $list[$term->term_id] = $term->name; $child_terms = get_terms( array( 'taxonomy' => array('tax-slug-1','tax-slug-2'), 'hide_empty' => false, 'parent' => $term->term_id //now we want this taxonomy's children ) ); if(! empty($child_terms)){ foreach($child_terms as $child_term){ $list[$child_term->term_id] = '- '.$child_term->name; } } } } // set choice list & return updated field $field['choices'] = $list; return $field; } |
There we go! Now you have a select field that allows the user to select between two ( or more ) taxonomies!