Home › Forums › General Issues › Pre-populate radio button with CPTs?
Is it possible to pre-populate a radio button field type list with another CPT and subsequently update that list if any new posts are added to the chosen CPT?
Yes, I am actually doing this with a dropdown, but it is the same concept. The Documentation can be found here: https://www.advancedcustomfields.com/resources/acf-load_field/.
You can use something like add_filter('acf/load_field/name=cpt_all', 'acf_load_post_types');
to limit the use to any custom fields named cpt_all. Here is the code that I am using:
function acf_load_post_types( $field ) {
// reset choices
$field['choices'] = array();
$args = array(
'public' => true
);
$post_types = get_post_types($args);
foreach ($post_types as $post_type) {
if ($post_type !== 'attachment' && $post_type !== 'page')
$field['choices'][$post_type] = ucfirst($post_type);
}
// return the field
return $field;
}
add_filter('acf/load_field/name=cpt_all', 'acf_load_post_types');
Hi cplumley, thank you for that. 🙂
I kind of understand but not sure how to link or target a specific field type?
For example i create a radio field called ‘services_select’ and that only appears on CPT a but it needs to auto pupulate from CPT b.
Where do i let it know that its targeting ‘services_select’ and populating from CPT b
Jez,
If I am understanding you correctly, you have a Custom Field in CPT b and you want CPT a to have access to the options that are selected?
To target a specific field in a CPT you would change this block
$args = array(
'public' => true
);
$post_types = get_post_types($args);
foreach ($post_types as $post_type) {
if ($post_type !== 'attachment' && $post_type !== 'page')
$field['choices'][$post_type] = ucfirst($post_type);
}
to something like
$cpt_custom_field = get_field($cpt_id, 'the_name_of_the_specific_field');
foreach ($cpt_custom_field['choices'] as $value=>$label) {
$field['choices'][ $value ] = [ $label ];
}
Or whatever you want to replace the information with. Nested repeater fields are slightly more complicated, but code examples for that are in the docs. You could create a colors array and assign the select to those, you can grab all post names from a CPT and assign those, etc. You just need to change the above code to match what you are loading into the choices field.
Hi cplumley
Kind of, i just want a list of checkbox buttons which list the CPTs, which you can select.
Dont need anything specifically from the CPT choice, well perhaps just its name and permalink.
So you’re on CPT a, and a side box has a list of checkboxes that is pre-populated with just the names of CPT b. Then front end on CPT a single page it shows the list of the ones you’ve chosen and a link to them.
The topic ‘Pre-populate radio button with CPTs?’ is closed to new replies.
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.