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.

 Answers

5

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: https://gist.github.com/MelMacaluso/6c4cb3db5ac87894f66a456ab8615f10

Wednesday, December 28, 2022
 
gpicazo
 
1

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

/wp-admin/edit.php

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
 
2

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: https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/

Headers:

Authorization:Bearer <token>
Content-Type:application/json

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

{
"title":"Sample ACF field demo",
"status": "publish",
"fields": 
    {      
        "text_custom_field_name" : "Text value",
        "checkbox_custom_field_name" : [
                "Option1,",
                "Option2,",
                "Option3"
            ],
        "textarea_custom_field_name" : "This is message field",
        "boolean_custom_field_name" : [
                true
            ]
    }
}

Please let me know if any help needed.

Thanks

Sunday, August 21, 2022
1

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
 
rafi
 
5

You can use depth serializer's option:

class statesSerializers(serializers.ModelSerializer):

    class Meta:
        model = states
        fields = '__all__'
        depth = 1
Thursday, September 22, 2022
 
waynec
 
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 :
 
Share

Browse Other Code Languages