1

In the documentation for /posts for WordPress REST API it states that I can add a meta-field. But it doesn't say which format that should be in.

I can find other guides showing how it should be if using Postman:

data = {
  "title": "test",
  "content": "testingfrompython",
  "status": "draft",
  "author": 1,
  "meta": {
    "location": "NYC",
    "date": "never",
    "event_url": "http: //google.com"
  },
  "featured_media": 1221
}

But how should it be setup, if I'm call the endpoint using PHP and cURL?


This here works for just creating a post:

$data = [
  'title'   => 'Test post',
  'status'  => 'draft', 
  'content' => 'Some content',
];
$data_string = json_encode( $data );

$endpoint = 'https://example.org/wp-json/wp/v2/posts';
$protocol = "POST";

$headers = [
  'Content-Type: application/json',
  'Content-Length: ' . strlen($data_string)
  'Authorization: Basic ' . base64_encode( 'MYUSERSEMAIL:APPLICATIONPASSWORD' )
];

$ch = custom_setup_curl_function( $endpoint, $protocol, $data_string, $headers );
$result = json_decode( curl_exec($ch) );

I've tried all kind of things:

Attempt1

$data = [
  'title'   => 'Test post',
  'status'  => 'draft', 
  'content' => 'Some content',
  'meta' => [
    'my_custom_field' => 'Testing 1234'
  ]
];

Attempt2

$data = [
  'title'   => 'Test post',
  'status'  => 'draft', 
  'content' => 'Some content',
  'meta' => [
    [
      'key' => 'my_custom_field', 
      'value' => 'Testing 1234' 
    ]
  ]
];

... And I could keep going. Every time it simply creates a post, but doesn't add any of the meta-data to my created custom fields.

I don't get why this is not stated in the documentation for the REST API.

2 Answers 2

0

I had to add this code from Stephen Mullen on StackOverflow to get it to work:

add_action( "rest_insert_user_question", function ( \WP_Post $post, $request, $creating ) 
{
    $metas = $request->get_param( "meta" );
    if( is_array( $metas ) ) {
        foreach( $metas as $name => $value ) {
            // update_post_meta( $post->ID, $name, $value );
            update_field( $name, $value, $post->ID );
        }
    }
}, 10, 3 );

Then I could call the endpoint like this:

$data = [
  'title'   => 'Test post',
  'status'  => 'draft', 
  'content' => 'Some content',
  'meta' => [
    'my_custom_field' => 'Testing 1234', 
  ]
];
1
  • Please include any relevant code in your answer, not just a link. Dec 29, 2020 at 2:11
0

First, you need to register the meta key in the REST API before the REST API can handle that meta data for the post/custom post. You can do this by using the methods register_post_meta link ↗ (a convenience wrapper for register_meta link ↗ useful for custom post types) or you can use register_meta directly.

Using the register_post_meta function, you can specify the post_type to assign the field to, the meta key and an array of properties, the important property is show_in_rest.

add_action('init', function(){

    register_post_meta(
        'my_custom_post_type',
        '_custom_meta_key',
        array(
            'single'       => true,
            'type'         => 'string',
            'show_in_rest' => true,
        )
    );
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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