2

I have several functions that are called from inside a save_post function. However, all of the functions that use the $post object are returning incorrect values because it appears that the default value being passed to save_post is the post ID rather than the post object.

How can I pass the post object to the save_post function in addition to the post ID?

add_action('save_post', 'my_save_function');

2 Answers 2

6

Do:

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

And the $post object will be passed as second argument to your function:

function my_save_function($post_ID, $post) {
16
  • @sorich: I'm getting an different (and incorrect) value for $post->ID echo inside the "my_save_function" than when its echo'd outside that function. Any ideas?
    – Scott B
    Commented Nov 1, 2010 at 17:14
  • Do you mean echo $post_ID and echo $post->ID return different results?
    – sorich87
    Commented Nov 1, 2010 at 17:19
  • Every time I test, the value of $post_ID, it gets incremented by one. Its as if this were a draft version ID incrementing. Any ideas?
    – Scott B
    Commented Nov 1, 2010 at 17:21
  • $post_ID and $post->ID are the same. However, the value being returned is in the 300's (increments by one each time i click "Publish" to test). My post id should be 176
    – Scott B
    Commented Nov 1, 2010 at 17:24
  • 1
    I am not sure, but I think it has something to do with the post revision. Are the values of $post->post_content and $post->post_title correct?
    – sorich87
    Commented Nov 1, 2010 at 17:33
1

Use get_post($post_id)

Reference:

http://codex.wordpress.org/Function_Reference/get_post

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.