POST meta fields to tags
Support › MB REST API › POST meta fields to tagsResolved
- This topic has 13 replies, 2 voices, and was last updated 6 years, 5 months ago by
mp81.
-
AuthorPosts
-
November 16, 2018 at 11:27 PM #11974
mp81
ParticipantHi 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.
November 17, 2018 at 11:13 AM #12032Anh Tran
KeymasterHi,
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 viameta_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 theterm 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 viameta_box
.I hope that makes sense.
November 18, 2018 at 5:11 AM #12042mp81
ParticipantHi 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.
November 18, 2018 at 5:55 AM #12043mp81
ParticipantIe how would you go about adding fields to the tags... your meta terms extension is enabled and so is the rest api extension.
November 19, 2018 at 5:42 PM #12057Anh Tran
KeymasterHi, let me check that again. The code for term and post are very similar.
November 19, 2018 at 7:02 PM #12059mp81
ParticipantI 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; } }
November 20, 2018 at 6:35 AM #12077mp81
ParticipantI 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.
November 20, 2018 at 10:36 PM #12102mp81
ParticipantHi Anh,
Any news with regards this?
Thanks,
November 22, 2018 at 5:14 AM #12134mp81
ParticipantJust 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.
November 22, 2018 at 7:50 AM #12135mp81
ParticipantFor 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.
November 22, 2018 at 10:22 AM #12139Anh Tran
KeymasterHi @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.November 22, 2018 at 2:32 PM #12147mp81
ParticipantHi 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
November 22, 2018 at 2:43 PM #12148Anh Tran
KeymasterOops, didn't notice it was spaces. I've just fixed it. Thanks for noticing me.
November 22, 2018 at 2:50 PM #12150mp81
ParticipantAwesome thanks. You can close this issue!
-
AuthorPosts
- You must be logged in to reply to this topic.