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
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.