How do I get fresh post data in post_publish hook?

Question

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
, cweber105 4 years 2019-04-18T11:41:44-05:00 0 Answers 64 views 0

Leave an answer

Browse
Browse