Support

Account

Home Forums ACF PRO ACF Load Field > Post Object > Render As Checkbox Reply To: ACF Load Field > Post Object > Render As Checkbox

  • Just in case it helps anyone…

    After a few minutes of thought, I figured that it was just way easier to dynamically populate a checkbox field with post values instead of superfluous wrangling. My code:

    
    function my_acf_field_checkbox( $field ){
    	
    	global $wpdb;
    	$querystr = "SELECT * FROM '$wpdb->posts' WHERE 'post_type' = 'my-cpt'";
    	$my_cpts = $wpdb->get_results($querystr, OBJECT);
    
    	if($my_cpts){
    		$my_cpt_arr = array();
    		foreach($my_cpts as $my_cpt):
    			$my_cpt_arr[$my_cpt->ID] = $my_cpt->post_title;
    		endforeach;
    	}
    	$field['choices'] = $my_cpt_arr;
    
    	return $field;
    };
    
    add_filter('acf/load_field/name=my_field_name', 'my_acf_field_checkbox');
    

    Hope this helps someone