I want to update/delete a post meta when a custom post (in this case custom post type is “booking”) is updated upon the change of post meta . In my case if user change the post meta booking_status
to ‘denied’ and then update the post, then I want to delete the post_meta booking_status
immediately .
Here is what I have tried
add_action( 'save_post', 'booking_status_is_updated' );
function booking_status_is_updated(){
global $post;
if($post->post_type =='booking'){
if(get_post_meta($post->ID,'booking_status',true)=='denied'){
delete_post_meta($post->ID,'booking_slot');
}
}
}
But this is not working ? How can I get it done ?
1 Answer
- Well, first use the hook properly. The post ID will be passed in. You
don’t need$post->ID
. - Second, use the correct hook. If you want to run
save_post
only for
your booking type, usesave_post_booking
But otherwise, the code works. I just ran a quick test.
function booking_status_is_updated($post_id){
if(get_post_meta($post_id,'booking_status',true)=='denied'){
delete_post_meta($post_id,'booking_slot');
}
}
add_action('save_post_booking','booking_status_is_updated');