0

I want to use save_post on an update for an existing post and also for a new post, but I want 2 different "priority" on the save_post action.

So, I can't check if it's an update into save_post function parameter, I have to check this before save_post.

How can I do this ?

Thanks

Ps: I want something like this :

add_action('save_post', function ($post_ID, WP_Post $post) { //Do something when it's an update }, 9, 2 ); add_action('save_post', function ($post_ID, WP_Post $post) { //Do something when it's a new post }, 11, 2 ); 
3
  • 2
    The save_post hook has a 3rd parameter, namely $update, so why not use an if-else from within a single callback to check whether it's an update or a new post? Why do you need two different priorities? Commented Feb 23, 2024 at 15:47
  • I already saw about $update parameter, but it will not affect priority parameter. I have to make a condition before being in save_post. Commented Feb 23, 2024 at 17:18
  • Out of curiousity what are you doing here? What's the priority 10 action you need to be the correct side of? Commented Feb 24, 2024 at 9:07

2 Answers 2

0

You can achieve this by using the wp_insert_post hook, which is called before the post is saved or updated. Inside this hook, you can check if the post ID exists to determine if it's an update or a new post. Here's how you can do it:

 add_action('wp_insert_post', function ($post_ID, $post, $update) { if (!$update) { // This is a new post add_action('save_post', function ($post_ID, $post) { // Do something when it's a new post }, 11, 2); } else { // This is an update add_action('save_post', function ($post_ID, $post) { // Do something when it's an update }, 9, 2); } }, 10, 3); 
0

You could register both and test the $update parameter before doing anything:

add_action('save_post', function ($post_ID, WP_Post $post, $update) { if (!$update) return; //Do something when it's an update }, 9, 3 ); add_action('save_post', function ($post_ID, WP_Post $post, $update) { if ($update) return; //Do something when it's a new post }, 11, 3 ); 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.