Use the acf/load_field filter to populate a select, checkbox, or radio field with dynamic options.
Populate a select field with titles from a custom post type:
<?php
add_filter( 'acf/load_field/name=related_service', function ( $field ) {
$field['choices'] = [];
$posts = get_posts( [
'post_type' => 'service',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
] );
foreach ( $posts as $post ) {
$field['choices'][ $post->ID ] = $post->post_title;
}
return $field;
} );
Populate with taxonomy terms:
<?php
add_filter( 'acf/load_field/name=product_category', function ( $field ) {
$field['choices'] = [];
$terms = get_terms( [
'taxonomy' => 'product_cat',
'hide_empty' => false,
] );
if ( $terms && ! is_wp_error( $terms ) ) {
foreach ( $terms as $term ) {
$field['choices'][ $term->term_id ] = $term->name;
}
}
return $field;
} );
Populate with user roles:
<?php
add_filter( 'acf/load_field/name=allowed_role', function ( $field ) {
$field['choices'] = [];
foreach ( wp_roles()->role_names as $role => $name ) {
$field['choices'][ $role ] = $name;
}
return $field;
} );
The acf/load_field filter works with select, checkbox, and radio button fields. Target a specific field by appending /name=field_name, /key=field_abc123, or /type=select.