1

I am running a function to do some stuff when save_post triggers to use new data. However, the $post object it returns, contains old data of the post, not new data! How can I get new post data inside my function? I have tried $WP_Query. But still, same old data!

This is how I am doing this:

function my_awesome_func ($post_id, $post, $update) {
    //my stuff
}
add_action( 'save_post_my-custom-post-type', 'my_awesome_func', 10, 3 );

Any thoughts?

4
  • Can you clarify what you mean by "use new data"? The save_post_* hook is fired once the the post is saved, but you have to handle anything else manually - for example saving post_meta
    – Welcher
    Commented Jan 27, 2016 at 4:13
  • By "new data" I meant, updated data of post meta. For example: i have one post meta, named "details". Now when I change value of "details" and update post, my function gets old value of "details", not the new set value. Commented Jan 27, 2016 at 9:41
  • @Welcher looks like your comment gave me some hint. Do I need to update post meta from my function? Something like this? Do I get data in my function like $_REQUEST['my-meta-key']? Commented Jan 27, 2016 at 9:45
  • yes that's exactly what you need to do.
    – Welcher
    Commented Jan 27, 2016 at 12:18

1 Answer 1

1

I'm going to post this as an answer, because it worked for me:

function my_awesome_func ($post_id, $post, $update) {
    //update the value we need early
    update_post_meta($post_id, 'my_meta_key', $_REQUEST['my_meta_key']);

    $newValue = get_post_meta($post_id, 'my_meta_key');
}
add_action( 'save_post_my-custom-post-type', 'my_awesome_func', 10, 3 );

You could also simply use the value of $_REQUEST['my_meta_key'] directly if that works, but for my sake I wanted to update the post immediately so I could get the value via another function, not use the value immediately.

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.