# Expose Chosen ACF Fields in the REST API

> Adds an "acf_fields" property with the ACF fields you list to the REST API responses of chosen post types, for headless front ends and apps.

- Type: PHP
- Runs: Run everywhere
- Category: [Custom Post Types & Fields](https://wpide.com/library/category/post-types/)
- Tags: acf, rest-api, headless
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/acf-rest-field-allowlist/

## Code

```php
// Post types and the field names to expose.
$wpide_acf_rest_types  = array( 'post', 'page' );
$wpide_acf_rest_fields = array( 'subtitle', 'price' );

add_action( 'rest_api_init', function () use ( $wpide_acf_rest_types, $wpide_acf_rest_fields ) {
    register_rest_field( $wpide_acf_rest_types, 'acf_fields', array(
        'get_callback' => function ( $object ) use ( $wpide_acf_rest_fields ) {
            $post_id = isset( $object['id'] ) ? (int) $object['id'] : 0;
            $values  = array();

            if ( ! $post_id || ! function_exists( 'get_field' ) || ( ! is_post_publicly_viewable( $post_id ) && ! current_user_can( 'read_post', $post_id ) ) ) {
                return $values;
            }

            foreach ( $wpide_acf_rest_fields as $name ) {
                $values[ $name ] = get_field( $name, $post_id );
            }

            return $values;
        },
        'schema'       => array(
            'description' => 'Selected ACF field values.',
            'type'        => 'object',
            'context'     => array( 'view', 'edit' ),
            'readonly'    => true,
        ),
    ) );
} );
```

## Notes

Only the listed fields are shared, and only for posts the visitor may read. ACF's own "Show in REST API" option exposes whole field groups instead.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Expose Chosen ACF Fields in the REST API" 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/
