I'm trying to populate a checkbox ACF field with the various post types of a WP site. This is for a plugin, so the post types will vary based on the location of install.
By default, the plugin uses pages and posts as it's post types, but need to give user option to use checkboxes to select other CPT's on the site. How can I populate the checkbox field with the list of all CPT's on a site. Here is my current section of PHP code to load the field within the plugin
array (
'key' => 'field_56e6d87b6c7be',
'label' => 'Add to Custom Post Types',
'name' => 'fb_pixel_cpt_select',
'type' => 'checkbox',
'instructions' => 'Select which Custom Post Types you would like to use.',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'choices' => array (
),
'default_value' => array (
),
'layout' => 'vertical',
'toggle' => 0,
),
You could use the ACF load field function here to auto populate your field. See more here: http://www.advancedcustomfields.com/resources/acfload_field/
Then, using the Wordpress get_post_types call (https://codex.wordpress.org/Function_Reference/get_post_types) you could retrieve those values and populate your field like this.
add_filter('acf/load_field/name=fb_pixel_cpt_select', 'acf_load_post_types');
function acf_load_post_types($field)
{
foreach ( get_post_types( '', 'names' ) as $post_type ) {
$field['choices'][$post_type] = $post_type;
}
// return the field
return $field;
}
Make sure that your field is already created before you try to populate it though.