0

I am trying to programatically create posts of a certain custom-post-type. Within the custom post type i've enabled 'show in rest api' and set a slug.

Within ACF i've setup a group that contains custom fields, and 'show this field if' is set to the custom post type. The fields show up on the created posts but they remain empty.

From my code:

            $response = $client->request('POST', 'mycustomposttype', [
            'json' => [
                'title'   => $title,
                'status'  => 'publish',
                'fields'  => 
                    [
                        'post_title' => $title,
                        'post_content' => '',
                        'post_excerpt' => 'Weer een leuke',
                        'post_status' => 'publish',
                        'post_type' => 'mycustomposttype',
                        'post_date'=> $datepublished
                    ]
                'meta' => /*my acf*/
                    [
                        'titel'             => $title,
                        'externalurl'       => $link,
                        'externalimageurl'  => $image,
                        'categorie'         => $category,
                        'country'           => $country,
                        'datetimepublished' => $datepublished
                    ]
            ]
        ]);

I thought i was wrong in naming the custom field type and have tried to name that array differently: meta_input, acf but the fields wont populate, maybe i'm searching wrong but i also can't find how it should be named ( still thinking this is whats going wrong).

It has to be something simple but i can't figure it out. Or can they only be populated when the post is already created?

2 Answers 2

0

Have a look here:

How to add/edit advanced custom fields on custom post type's WordPress REST API?

You need a little bit of extra code to hook up the Wordpress REST API to the ACF functions to get and write ACF fields. Then with that, you can access the fields directly like other 'regular' post type fields (i.e. put them at the same level as title and status).

I've taken the example from that answer and tweaked it for your case, but you'll need to test and debug it and you need to implement this for all your fields, not just titel:

function rest_api_mycustomposttype_titel() {
   register_rest_field( 'mycustomposttype', 'titel', array(
        'get_callback' => function(){ return get_field('titel'); },
        'update_callback' => function( $value, $post ){
            update_field('titel', $value, $post->ID );
        }
   ));
}

add_action("rest_api_init", 'rest_api_mycustomposttype_titel');

I also referred to this page, which notably does not reference this case, so I'm assuming that ACF still does not provide the functionality you need here 'automatically'. It does reference POSTing to the ACF API to update the fields for an existing post, so that's another option, to do it as two calls.

3
  • Thanks, the support for REST API has been updated. but besides accessing and editing i see no changes for doing this in one request. Thankfully i also populate a database table with the same fields, so i have a backup solution to write a second script to take care of that. Weird still that it's missing support for doing this
    – RkdL
    Jun 30, 2023 at 17:48
  • @RkdL This is the solution to do it in one request
    – mozboz
    Jun 30, 2023 at 17:50
  • maybe on me to not provide more code, but the solution to work with the array as in my question was to a: provide the Content-Type : application/json header , and subsequently changing the array name from meta back to the working acf. I was then able to send the request in one go the endpoint at 'base_uri' => 'https://wordpresswebsite.nl/wp-json/wp/v2/
    – RkdL
    Jul 6, 2023 at 17:56
0

To be able to create a new post remotely, through the wordpress rest api, i forgot to format my request properly. In my case, when setting up my client variables within php, i failed to include 2 things.

1: the "Content-Type : application/json" header.

$client = new Client([
                'base_uri' => 'https://WordpressSite.nl/wp-json/wp/v2/',
                'timeout' => 2.0,
                'auth' => ['WPusername', 'WPpassword'],
                'headers' => [
                    'Content-Type' => 'application/json'
                ]
            ]);

2: provide the right name for the array that holds the acf key:value pairs . While i've tried acf: before, it wasn't accepted until after i added the above header.

try {
        $response = $client->request('POST', 'customposttypename', [
            'json' => [
                'title'   => $source . " " . $title,
                'status'  => 'publish',
                'fields'  => [
                        'post_title' => $source . " " . $title,
                        'post_content' => '',
                        'post_excerpt' => 'Weer een leuke',
                        'post_status' => 'publish',
                        'post_type' => 'customposttype',
                        'post_date'=> $datepublished
                    ],
                'acf'   =>
                    [
                        'titel'             => $source . " " . $title,
                        'externalurl'       => $link,
                        'externalimageurl'  => $image,
                        'categorie'         => $category,
                        'country'           => $country,
                        'datetimepublished' => $datepublished,
                        'source_website' => $source
                    ]
            ]
        ]);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.