Asked  2 Years ago    Answers:  5   Viewed   537 times

I want to expose all the ACF fields that belong to a page or custom post type to the WordPress REST API in order to do some API calls through javascript.

The final expected result would be all the ACF fields inside an ACF object that you can easily access.



Through the following code, you will be able to expose page and your custom postypes ACF fields in the wordpress REST API and access them inside the ACF object.

You can obviously customise the postypes to exclude or to include in the arrays: $postypes_to_exclude and $extra_postypes_to_include.

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' );

Here's the gist for reference:

Wednesday, December 28, 2022

A quick and dirty way to do it would be to find this file:


And comment out the following line:

echo '<div id="message" class="updated notice is-dismissible"><p>' . join( ' ', $messages ) . '</p></div>';

Of course that will get rid of all your 'moved to Trash' messages. So you'll probably want to keep it in there, but check for your custom post type first.

Or better yet, figure out how to override this from your functions.php file so the next WP update doesn't kill it.

Thursday, September 15, 2022

Like the GET request, You can use the POST request as well to store data into the CMS. What you need to do is to pass the authorization headers with the POST API call.
You can get more detail about the authorization mechanism here:


Authorization:Bearer <token>

Secondly you can pass the Body data as a RAW json as below:

"title":"Sample ACF field demo",
"status": "publish",
        "text_custom_field_name" : "Text value",
        "checkbox_custom_field_name" : [
        "textarea_custom_field_name" : "This is message field",
        "boolean_custom_field_name" : [

Please let me know if any help needed.


Sunday, August 21, 2022

To add rows into sub-fields of a repeater field, you can do:

$field_key = "repeater_field";
$user_id = "user_123"; // save to user (user id = 123)

$value = get_field($field_key, $user_id);
$value[] = array("event_id " => 25);
$value[] = array("event_id " => 30);
update_field( $field_key, $value, $user_id );

To remove items, I'd use a PHP function such as the one in the accepted answer at Delete element from multidimensional-array based on value:

$value = removeElementWithValue($value, "event_id", 25);
update_field( $field_key, $value, $user_id );

As to where to add these codes (ie. the action hook) depends on when you'd like to add/remove the items.

Thursday, September 22, 2022

You can use depth serializer's option:

class statesSerializers(serializers.ModelSerializer):

    class Meta:
        model = states
        fields = '__all__'
        depth = 1
Thursday, September 22, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :

Browse Other Code Languages