admin管理员组

文章数量:1410712

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
}

本文标签: pluginsHow do I get fresh post data in postpublish hook