Support » Fixing WordPress » Tags are connected to article after `save_post` webhook?

  • Resolved TagBee

    (@tagbee)


    Hi everybody,

    I need someone’s help on something.

    Using wp_get_post_tags($id) within save_post webhook was working fine in previous wordpress versions.

    In the latest one it seems like tags are persisted right after save_post hook.

    wp_get_post_tags($id) bring back the old set of tags and not the updated one when a user adds a new tag.

    Any hints?

    Thank you in advance!

    • This topic was modified 3 years, 2 months ago by TagBee.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    With the block editor (Gutenberg), post tags are updated via the REST API. This does occur just before the post is saved, but because the tags are saved in another process, you can run into a race condition when using “save_posts” where the action fires before the API update process can finish. How you get around this condition would depend on why you need the new tags and what you plan to do with the information.

    Thread Starter TagBee

    (@tagbee)

    Thank you for your reply.

    Well what I am trying to do is on save_post get all post tags and send them to an external service.

    However using wp_get_post_tags($id) brings me back the old post tags.

    Is there any way to get the updated one (the final user’s selection) within save_post?

    If you want to have a look in the code you can check here: https://github.com/tagbee/wordpress-plugin

    Thank you in advance!

    PS. That approach was working find for version 4.*

    Moderator bcworkz

    (@bcworkz)

    It looks like I misspoke about tags being updated before save_post action. That remains true for classic editor users. When posts are updated via the block editor using the API, tags are updated after save_post.

    Try getting the tags from the “rest_after_insert_post” action. Now the problem would be still supporting the classic editor at the same time. Maybe you need to hook both actions. If rest_after_insert_post never fires but save_post does, classic editor is used so it’s safe to use the wp_get_post_tags values from save_post. If both fire, you must use the the wp_get_post_tags values from the rest_after_insert_post action.

    I’m not quite sure how to actually implement that. Now that you know how to get values for each condition, I think you will be able to work out something. I’ve not tested if rest_after_insert_post will meet your needs, but I don’t see why it wouldn’t. What I do know is the tag IDs are POSTed to wp-json/wp/v2/posts/{$ID} in order to update assigned tags. Thus WP_REST_Posts_Controller is involved. If rest_after_insert_post does not work as expected, that class is where you will want to investigate further. If you still have trouble, let me know.

    Thread Starter TagBee

    (@tagbee)

    Many thanks!

    Already updated plugin code – according to what you mentioned in your previous message – and it works like a charm.

    Thank you again!

    Moderator bcworkz

    (@bcworkz)

    Awesome! You’re welcome.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Tags are connected to article after `save_post` webhook?’ is closed to new replies.