Forked from MelMacaluso/expose_ACF_fields_to_REST.php
Created
August 27, 2020 09:14
-
-
Save onur-km/08d767cc9f7657eb069e905e756a9f86 to your computer and use it in GitHub Desktop.
Automatically expose all the ACF fields to the Wordpress REST API in Pages and in your custom post types.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function create_ACF_meta_in_REST() { | |
$postypes_to_exclude = ['acf-field-group','acf-field']; | |
$extra_postypes_to_include = ["page"]; | |
$post_types = array_diff(get_post_types(["_builtin" => false], 'names'),$postypes_to_exclude); | |
array_push($post_types, $extra_postypes_to_include); | |
foreach ($post_types as $post_type) { | |
register_rest_field( $post_type, 'ACF', [ | |
'get_callback' => 'expose_ACF_fields', | |
'schema' => null, | |
] | |
); | |
} | |
} | |
function expose_ACF_fields( $object ) { | |
$ID = $object['id']; | |
return get_fields($ID); | |
} | |
add_action( 'rest_api_init', 'create_ACF_meta_in_REST' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment