最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom post types - How to get updated data when save_post triggers?

programmeradmin1浏览0评论

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?

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?

Share Improve this question asked Jan 26, 2016 at 19:15 тнє Sufiтнє Sufi 1,7104 gold badges19 silver badges28 bronze badges 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. – тнє Sufi 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']? – тнє Sufi Commented Jan 27, 2016 at 9:45
  • yes that's exactly what you need to do. – Welcher Commented Jan 27, 2016 at 12:18
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论