0

In admin, I need to fire the save_post action before firing the transition_post_status hook. But according to this, the transition_post_status fires first before the post is saved.

How can I reverse it? I tried changing the priority order below, but I don't think it works cos it's for different functions firing for the same action.

$this->loader->add_action( 'save_post', $plugin_admin_listings, 'save_meta_data', 9, 2 );
$this->loader->add_action( 'transition_post_status', $plugin_admin_listings, 'transition_post_status', 10, 3 );
7
  • 1
    No, I don't think so. What are you trying to do? Can't you run the save meta action on transition post status hook too?
    – Rup
    Commented Jan 3, 2022 at 13:35
  • 1
    what is the problem that you're hoping to solve by doing this? This is not something that can be done
    – Tom J Nowell
    Commented Jan 3, 2022 at 13:43
  • Ok. On this plugin that I'm modding, an email is sent to the user when the transition_post_status action is fired (draft -> published). But I want to fire it only after the save_post action since I want some data to be firstly saved and then email that data to the user. I can't do the emailing in the save_post since each time the post is saved, the email is fired. I only wish to fire when the post is published.
    – ratib90486
    Commented Jan 3, 2022 at 14:00
  • If you only want to run the code when the post is published but not when it's updated then that is something that can be solved and has been asked about on this site before. Changing the order of the hooks though is not possible, and if it were it would cause new problems unrelated to your task
    – Tom J Nowell
    Commented Jan 3, 2022 at 14:41
  • @TomJNowell Yes, but the problem is I need the post to be saved first cos in I'm updating its post meta. Then, when it gets published, my site should trigger the transition_post_status to send the email to the user tell them about the post meta value. Or do you think I can use js to do this, like get transition_post_status to read the value from the input text field?
    – ratib90486
    Commented Jan 4, 2022 at 3:02

2 Answers 2

0

It's a bit of a hack, but you could

  • create a new member in your $plugin_admin_listings class that's an array of post IDs published in this page cycle
  • in your transition_post_status add the IDs to that array
  • in save_meta_data as you save each post check if its ID is in the published_posts array and then send the email then

You probably need an array not a single ID in case this can be triggered from an admin bulk action (I'd guess so?). However this falls apart though if the two hooks run in separate page loads (I'd guess they don't though). If that was the case you could e.g. add two post_meta timestamps

  • last published timestamp (set in transition post status)
  • last published email sent timestamp (set when you send the email)

In your save_post hook you could check if the first is after the second, and then send a new email. (And it's also possible one of the existing dates on the WP_Post object would work too, but I forget precisely when they're set.)

0

Ok, I "solved" it by placing my intended code (update_post_meta) inside the function that is hooked by transition_post_status.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.