5

Is there a way to trigger the post saving or updating event in code? such as do_action('save_post', $post_id); The function here seems to imply that a WP_POST object needs to be passed in. Is there a way to basically imitate the action of updating the post with all its existing values. The point is for the other hooks linked to all trigger on post update. Maybe do a wp_insert_post(), just passing in the post id?

https://developer.wordpress.org/reference/hooks/save_post/
do_action( 'save_post', int $post_ID, WP_Post $post, bool $update )

8
  • Not quite following "other hooks linked to all trigger" - could you clarify what you're trying to accomplish? It sounds like more than just adding a new revision to the db.
    – WebElaine
    Commented Apr 24, 2017 at 19:33
  • 1
    I want to trigger all the hooks hooked to save_post that's why I'd like to trigger the event somehow. Commented Apr 24, 2017 at 21:02
  • What is your goal in triggering all of the hooks? If you explain a bit more we may be able to help you think of another solution that's cleaner.
    – WebElaine
    Commented Apr 25, 2017 at 15:09
  • Other plugins have hooks linked to save_post that I want to trigger. Commented Apr 25, 2017 at 15:44
  • why you don't want to use the function wp_insert_post ?
    – mmm
    Commented Apr 25, 2017 at 19:48

2 Answers 2

0

It's wp_insert_post() vs. wp_update_post() - where update will ultimately also call:

return wp_insert_post( $postarr, $wp_error, $fire_after_hooks );

The term "once" implies that it is being fired "afterwards".

/**
 * Fires once a post has been saved.
 *
 * @since 1.5.0
 *
 * @param int     $post_ID Post ID.
 * @param WP_Post $post    Post object.
 * @param bool    $update  Whether this is an existing post being updated.
 */
do_action( 'save_post', $post_ID, $post, $update );

When inserting a post, save_post is being fired while $fire_after_hooks is true.
And usually one may want to insert/update the record and not only fire the hook ...

2
  • This doesn't answer OPs question - you're implying they need to pass a $poastarr in, when OP is asking if you can save existing values as is.
    – AlxVallejo
    Commented Jan 26, 2022 at 15:05
  • 1
    @AlxVallejo do_action('save_post', $post_id) is non-sense, when no data is being passed (one can register whatever one wants). But there's generally not much use in firing hooks, without the corresponding data payload. I mean, $post_ID should refer to an existing post. Commented Jan 26, 2022 at 16:02
-1

The answer to your question is to call do_action with the correct parameters. However, I would like to know more about why you're doing this - is there a post your saving programmatically?

3
  • 4
    There are multiple hooks in the system related to save_post event that I want to trigger for the posts. Commented Apr 25, 2017 at 13:32
  • Not sure why the downvote. The best way to trigger all the actions related to save post is to call either wp_insert_post or wp_update_post
    – Welcher
    Commented Dec 1, 2019 at 14:29
  • 2
    I think it was downvoted because it doesn't provide much insight into the problem and it could just as well have been a comment rather than an answer.
    – admcfajn
    Commented May 14, 2020 at 3:30

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.