0

I was trying to figure out why save_post wasn't being fired upon 'Quick edit', and found the solution here - apparently global $post is not available when updating a post in Quick edit mode.

So - I got it working now - see working example below, but I don't understand why it's working.

function mytestfunc($post_id) 
{
    $key = 'test';
    $data = 'this is a test';
    update_post_meta( $post_id, $key, $data );
}
add_action( 'save_post', 'mytestfunc' );

Can someone explain to me where this $post_id is coming from? I'm not passing the value in add_action - so I don't understand why mytestfunc knows which post I'm targeting?

2

It comes from wp_insert_post(), where do_action() is called with two additional parameters:

do_action('save_post', $post_ID, $post);

So it is not you who adds the parameters, it is WordPress.

If you register your callback with the fourth parameter set to 2

add_action( 'save_post', 'mytestfunc', 10, 2 );

… you will even get the complete $post object:

function mytestfunc( $post_id, $post ) 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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