0

I have a metafields that is assigned to a custom post which I added as a REST API field:

$this->post_types = array ( 'post', 'location' );

foreach( array( 'field_1', 'field_2' ) as $field ) {
    register_rest_field( $this->post_types,
        $field,
        array(
            'get_callback'    => array( $this, 'get_post_meta' ),
            'update_callback' => array( $this, 'update_post_meta' ),
            'schema'          => null,
        )
    );
}

And I also have the methods:

public function get_post_meta($object, $field_name, $request) {
    return get_post_meta( $object['id'], $field_name, true );
}

public function update_post_meta($value, $object, $field_name) {
    return update_post_meta( $object->ID, $field_name, $value );
}

If I test adding a new post/update and value to field_1 and field_2 through Postman, it works fine.

But when I try to add a value to the metafields, they don't seem to get added when I use WP-APi/node-wpapi. Here is a snippet of the code I have:

 response = await this.wordPress.cv_location().create({
  title: article.title,
  content: article.content,
  status: article.status,
  meta: {
    field_1: article.field_1,
    field_2: article.field_2,
  }
});

I notice that if I use post() instead of cv_location(), the metafields get populated correcrtly with the values but the post gets added to the post post type and not the CV Location post type.

Thanks in advance to any suggestion.

  • try with wp.types().type('location').create(... – mmm Mar 20 '18 at 17:53
  • @mmm it still doesn't work. – Jeda Dragon Mar 21 '18 at 11:28

Your Answer

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

Browse other questions tagged or ask your own question.