I'm looking to use the bitly api to generate a shortened url for all posts and pages.
My plan is to generate this when the post is saved, then save the shortened to the post when the API call is answered. But I'm worried that if I use the wrong hook I'll end up in an infinite loop of listening for the update hook, then triggering the update hook.
So how can I avoid this, or is there a hook I can use that will trigger when a post is updated but wouldn't trigger when I add meta data to a post
I'm looking to use the bitly api to generate a shortened url for all posts and pages.
My plan is to generate this when the post is saved, then save the shortened to the post when the API call is answered. But I'm worried that if I use the wrong hook I'll end up in an infinite loop of listening for the update hook, then triggering the update hook.
So how can I avoid this, or is there a hook I can use that will trigger when a post is updated but wouldn't trigger when I add meta data to a post
Share Improve this question edited Oct 25, 2019 at 19:49 Cole asked Oct 25, 2019 at 18:50 ColeCole 32 bronze badges1 Answer
Reset to default 0Use the hook 'save_post' that pass 1 arguments: $post_id. Update_post_meta won't trigger save_post.
add_action('save_post', 'custom_add_meta', 10, 1);
function custom_add_meta($post_id){
update_post_meta($post_id, 'meta_key', $value);
}