Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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');
share|improve this question
add comment

2 Answers 2

up vote 4 down vote accepted

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) {
share|improve this answer
    
@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 Nov 1 '10 at 17:14
    
Do you mean echo $post_ID and echo $post->ID return different results? –  sorich87 Nov 1 '10 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 Nov 1 '10 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 Nov 1 '10 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 Nov 1 '10 at 17:33
show 12 more comments

Use get_post($post_id)

Reference:

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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