最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

What hook should I use to add post meta data with on update?

programmeradmin1浏览0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 0

Use 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);
}
发布评论

评论列表(0)

  1. 暂无评论