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

plugins - How do I get fresh post data in post_publish hook?

programmeradmin0浏览0评论

I'm using the post_publish hook to do some operations in my plugin based on the post's taxonomies. If update a post, calling get_the_category($id) returns the categories for the post version before the update, not after it, which messes up the operations I'm running.

For example, if I publish a post with category id's 20 and 21, and try to get the post's categories in the post_publish hook, it works. If I then edit the post so it only has category 20, and try to the get categories in the post_publish hook, it returns categories 20 and 21, even though the update only has category 20. If I edit the post title, that is accurately reflected in the $post argument.

I have read this similar question, but that one concerns meta values, not taxonomies, and I'm not sure how to apply it's answer here.

How do I get the fresh taxonomy data? This also happens if I use the save_post hook.

add_action("publish_post", "process_published_post", 10, 2);

function process_published_post($id, $post) {
    $categories = get_the_category($id); 
    //Categories here are old when post is being updated
}
发布评论

评论列表(0)

  1. 暂无评论