Support

Account

Home Forums Feature Requests REST API Reply To: REST API

  • Just a small update on this.
    I ended up not using a plugin at all, but instead just adding the ACF fields by batch like this:

    add_action( 'rest_api_init', 'slug_register_acf' );
    function slug_register_acf() {
      $post_types = get_post_types(['public'=>true], 'names');
      foreach ($post_types as $type) {
        register_api_field( $type,
            'acf',
            array(
                'get_callback'    => 'slug_get_acf',
                'update_callback' => null,
                'schema'          => null,
            )
        );
      }
    }
    function slug_get_acf( $object, $field_name, $request ) {
        return get_fields($object[ 'id' ]);
    }
    

    This appends a key called “acf” to the rest api output for all public post types.
    Will be easier to customise too if you only want certain fields to be exposed.