# Fill an ACF Select Field With Posts of a Type

> Loads the choices of an ACF select field from the titles of a post type, such as locations or team members, and refreshes them when those posts change.

- Type: PHP
- Runs: Run everywhere
- Category: [Custom Post Types & Fields](https://wpide.com/library/category/post-types/)
- Tags: acf, select, dynamic
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/acf-select-choices-from-posts/

## Code

```php
// Select field name and the post type supplying its choices.
$wpide_acf_choice_field = 'location';
$wpide_acf_choice_type  = 'page';

add_filter( 'acf/load_field/name=' . $wpide_acf_choice_field, function ( $field ) use ( $wpide_acf_choice_type ) {
    $key     = 'wpide_acf_choices_' . $wpide_acf_choice_type;
    $choices = get_transient( $key );

    if ( false === $choices ) {
        $choices = array();
        $ids     = get_posts( array( 'post_type' => $wpide_acf_choice_type, 'post_status' => 'publish', 'posts_per_page' => 200, 'orderby' => 'title', 'order' => 'ASC', 'fields' => 'ids', 'no_found_rows' => true ) );

        foreach ( $ids as $id ) {
            $choices[ $id ] = get_the_title( $id );
        }

        set_transient( $key, $choices, DAY_IN_SECONDS );
    }

    $field['choices'] = $choices;

    return $field;
} );

add_action( "save_post_{$wpide_acf_choice_type}", function () use ( $wpide_acf_choice_type ) {
    delete_transient( 'wpide_acf_choices_' . $wpide_acf_choice_type );
} );

add_action( 'deleted_post', function ( $post_id, $post ) use ( $wpide_acf_choice_type ) {
    if ( $post && $wpide_acf_choice_type === $post->post_type ) {
        delete_transient( 'wpide_acf_choices_' . $wpide_acf_choice_type );
    }
}, 10, 2 );
```

## Notes

Set the field name and the source post type. The list is cached until a post of that type is saved or deleted.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Fill an ACF Select Field With Posts of a Type" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
