我目前有一个自定义元字段,用于向帖子添加视频url。如果元字段有任何值,我希望现有的分类术语“视频”在保存后自动分配给帖子。
如果存在自定义元值,则自动分配分类术语
1 个回复
最合适的回答,由SO网友:Andrew Bartel 整理而成
你应该钩住save_post 行动
add_action( \'save_post\', \'add_video_taxonomy\' );
function add_video_taxonomy( $post_id ) {
// you should check if the current user can do this, probably against publish_posts
// you should also have a nonce check here as well
if( get_post_meta( $post_id, \'video_url\', true ) ) {
wp_set_post_terms( $post_id, \'video\', \'your_custom_taxonomy_name\', true );
}
}
结束