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
  • Have you checked $_POST or $_REQUEST as your linked related question suggests in the answers? – Howdy_McGee Apr 18 '19 at 16:46
  • 1
    Yes, but neither includes the post's taxonomies. – cweber105 Apr 18 '19 at 16:51
  • If I try this on a normal post, assign categories and print out the $_POST there's a key for post_category which has an array of assigned category IDs. Does this not happen with custom taxonomies? – Howdy_McGee Apr 18 '19 at 17:01
  • I don't see that field. I am using one custom taxonomy, and this is a regular post type. This is the print_r of $_POST and $_REQUEST - imgur.com/a/ojAlczQ – cweber105 Apr 18 '19 at 17:59
  • 1
    Is it tax_input? Is your taxonomy slug lift_theme? – Howdy_McGee Apr 18 '19 at 18:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.