我正在尝试使用作者元数据来自动填充给定作者帖子的自定义分类法。目前,分类法在帖子页面上是可见的,但我打算将其隐藏在前端,以避免意外的用户选择。(也是预填充的原因)
注意:关联的分类术语和作者元数据是相同的(作者元数据选项是从现有的分类术语中动态提取的)。
出于某种原因——这不起作用。我绞尽脑汁想弄清楚为什么——我认为逻辑是正确的。我是不是错过了什么/有人能帮忙吗?
提前谢谢。
function update_school($post_id) {
$post_author_id = get_post_field( 'post_author', $post_id ); // get the post author ID
$school_title = get_the_author_meta( 'school2', $post_author_id ); // from the post author ID, get the author meta
$school_slug = get_the_author_meta( 'school', $post_author_id ); // from the post author ID, get the author meta
$school_term = get_term_by('slug', $school_slug, 'school'); // get term by the author meta called "school"
$school_term_name = $school_term->name; // from the slug, get the school term name
// update the post taxonomy "school" with author meta school variables above
wp_set_post_terms( $post_id, $school_term_name, 'school' );
}
// run function on post save
add_action('save_post', 'update_school');
为任何想知道的人都想好了:
function update_school( $post_id ) {
$post_author_id = get_post_field( 'post_author', $post_id ); // get the post author ID
$school_name = get_the_author_meta( 'school2', $post_author_id ); // from the post author ID, get the author meta
$term = term_exists( $school_name, 'school');
wp_set_post_terms( $post_id, $term, 'school' );
}
// run function on post save
add_action( 'save_post', 'update_school' );