1

I have a stand alone app done in PHP which creates a new post on my remote WordPress site. Everything works with exception of adding meta data / custom fields to the post at the time when I create it. Looking at the end point for posts I see meta is an object and am trying to pass a key->value array. Example of what I'm posting

$post=array('title' => 'Test Title',
     'content' => 'I like short test posts.',
     'author' => 1,
     'excerpt' => 'This could be longer than the actual post',
     'categories' => array(2,3,4,5),
     'meta' => array('MyMetaKey' => 'MyMetaValue')
     );

Checking the post on the site everything is in there except for the meta data/custom fields. Not sure the post array is correct.

1

You need to register your meta field first, then you can insert / update it into a post using rest api. https://codex.wordpress.org/Function_Reference/register_meta

$object_type = 'post';
$args1 = array(
    'type' => 'string', 
    'description' => 'My Meta Key Description',
    'single' => true,
    'show_in_rest' => true,
    );
register_meta( $object_type, 'MyMetaKey', $args1 );

Hope it helps

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.