0

I have a function hooked to the save_post hook. this function is mostly concerned with the categories that the post is in. My function does the following:

function post_save_hook($post_id){
    $categories = get_the_category($post_id);
    foreach($categories as $category){
        //Handle this category
    }
}

Say I have a category called featured, and a post was published under this category. then the same post was edited and removed from the featured category, in this case my function won't detect this change.

So what I want is to know if this post was in some category and removed from it.

  • When do you need to know? Do you need a long term record, or do you just need to know when the category is removed? – s_ha_dum Jun 3 '14 at 13:41
  • No, I just need it this one time that the post was removed from this category. – Khalid Dabjan Jun 3 '14 at 13:52
0

save_post runs too late to do what you are trying to do. That hook fires after the post and related meta data are stored. The category has already been removed at that point, and WordPress keeps no record.

You will need to hook into the save process earlier, perhaps pre_post_update:

add_action(
  'pre_post_update',
  function($post_ID,$data) {
    var_dump($post_ID,$data);
    var_dump(get_the_category($post_ID));
    die;
  },
  10,2
);

Proof of concept code only, obviously.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.