You can go two ways:
- There is probably not much data for the taxonomies of both CPT’s, so you could render these two arrays in javascript from php in your template, and then switch the dropdown data on click of the checkbox. You could have the arrays named with the id’s of the CPT’s, e.g.
cpt-134
and then on click the CPT, set the dropdown data to the array namedcpt-[id-of-cpt]
. One way to get this data into your javascript is, if you e.g. have ageneral.js
:
wp_register_script('general', get_template_directory_uri() . '/js/general.js');
wp_localize_script('general', 'myVars', array(
'site_url' => site_url(),
'dropdownData' => get_CPT_Dropdown_Data(),
));
wp_enqueue_script('general');
You would ofcourse have to write that function get_CPT_Dropdown_Data()
that returns the arrays with the taxonomy data, based on the CPT id’s.
- On click on the CPT, call an ajax function that you have made that retrieves the taxonomies for that CPT. Then update the dropdown with that data.
Option 2 is a more future-proof method (if things get bigger) and will not bloat your code with data that might not be needed. But it will add round trips to the server, so you might want to incorporate feedback for your users for when the loading starts.