Support

Account

Home Forums General Issues Pre-populate radio button with CPTs?

Solving

Pre-populate radio button with CPTs?

    • jez

    • February 12, 2020 at 3:16 am

    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');
    • jez

    • February 12, 2020 at 7:30 pm

    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.

    • jez

    • February 13, 2020 at 8:57 pm

    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.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.

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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.