Support Forum
Support › MB REST API › POST meta fields to tagsResolved
Hi guys,
I am trying to do a POST request to wp-json/wp/v2/tags to create a new tag, and as part of the POST request I am sending as a form parameter is 'meta_box' with a json value.
My tag gets created but my field (field id is _post_tag_aliases, it is a text field) does not get populated
I have attempted various formats for the meta_box value:
[
{
"_post_tag_aliases":"abc"
}
]
and
{
"_post_tag_aliases":"abc"
}
And I have not had any success.
Would love to know how I can achieve this. I have not been able to find any examples of doing this.
Hi,
I'm not sure what your field type is. So depending of the field type, we have different solution.
For taxonomy
field, as the plugin doesn't store value in post meta, there's no need to submit value via meta_box
field. Instead, after creating tags, you should send another request to set post terms, e.g. set the created tags as a term for the post.
For taxonomy_advanced
field, the value saved in the DB is the term IDs
(separated by commas), not term slugs. So I think you still need to send 2 requests: one for creating tags and gets their IDs, one for adding those IDs to post meta via meta_box
.
I hope that makes sense.
Hi Anh,
Thanks for a quick reply.
I don't think I made myself clear however.
I am trying to submit A POST request via REST api. According to the docs for rest api https://docs.metabox.io/extensions/mb-rest-api/ to submit field values, one must pass in a field called meta_box.
This works fine for creating a post. However, when using your terms extension, and passing in the meta_box parameter, the same does not work.
Ie how would you go about adding fields to the tags... your meta terms extension is enabled and so is the rest api extension.
Hi, let me check that again. The code for term and post are very similar.
I have been checking this further, and for me at least, in class-mb-rest-api.php the update_term_meta update_callback never gets triggered.
This is when I use for the tags endpoint:
POST to /wp-json/wp/v2/tags with parameters:
name: A tag
slug: a-tag
description: A tag
meta_box: { 'an_existing_field': 'abc' }
Result: update_term_meta NOT TRIGGERED
This is when I use for the posts endpoint:
POST to /wp-json/wp/v2/posts with parameters:
title: A tag
slug: a-tag
description: A tag
meta_box: { 'an_existing_field': 'abc' }
Result: update_post_meta TRIGGERED
Btw, you have the following code inside both update_term_meta and update_post_meta
if ( is_string( $data ) ) {
$data = json_decode( $data, true );
if ( JSON_ERROR_NONE === json_last_error() ) {
return;
}
}
I believe this should instead be
if ( is_string( $data ) ) {
$data = json_decode( $data, true );
if ( JSON_ERROR_NONE !== json_last_error() ) {
return;
}
}
I have investigated this further and I found out that saving meta_box data only does not work for builtin taxonomies: so for post_tags and categories.
If I register a custom taxonomy, everything works as it should.
Any ideas why meta fields for builtin taxonomies do not save? and the update_term_meta never triggers for them, while it triggers fine for a custom taxonomy.
Hi Anh,
Any news with regards this?
Thanks,
Just an update for this for anyone following, I found a partial solution that fixes posting custom taxonomies with meta_box parameter via rest api.
In file mb-rest-api/class-mb-rest-api.php line 155 is
$field = rwmb_get_registry( 'field' )->get( $field_id, $object->taxonomy);
It should in fact be:
$field = rwmb_get_registry( 'field' )->get( $field_id, $object->taxonomy, 'term' );
I will continue investigating why builtin taxonomies do not get their meta_box fields passed in and saved at all.
For anyone following, I have found a fix for the post_tag not having it's meta values saved as well.
In order to fix this one must edit mb-rest-api/class-mb-rest-api.php and change
`register_rest_field(
$this->get_types('taxonomy'),
'meta_box',
array(
'get_callback' => array( $this, 'get_term_meta' ),
'update_callback' => array( $this, 'update_term_meta' ),
)
);`
to
$taxonomies = $this->get_types('taxonomy');
if (in_array("post_tag", $taxonomies)) {
$post_tag_key = array_search('post_tag', $taxonomies);
$taxonomies[$post_tag_key] = 'tag';
}
register_rest_field(
$taxonomies,
'meta_box',
array(
'get_callback' => array( $this, 'get_term_meta' ),
'update_callback' => array( $this, 'update_term_meta' ),
)
);
This is because there appears to be a bug in WordPress rest api itself where it checks for 'tag' instead of 'post_tag' when looking for additional fields.
Hi @mp81. Thanks a lot for your help. I've merged your PR on Github and added the fix for post_tag
. Please take a look. If that's ok, I'll release a new version.
Hi Anh,
Thanks for merging. https://github.com/wpmetabox/mb-rest-api/blob/master/class-mb-rest-api.php looks ok now except you have some indentation issues on lines 56 to 62.
best regards
Oops, didn't notice it was spaces. I've just fixed it. Thanks for noticing me.
Awesome thanks. You can close this issue!