繁体   English   中英

是否自动根据邮政作者元数据填充邮政分类数据?

[英]Automatically Populate Post Taxonomy Data Based on Post Author Meta Data?

我正在尝试使用作者元数据自动在给定作者的帖子上填充自定义分类法。 当前,分类法在帖子页面上可见,但我打算将其从前端隐藏起来,以避免用户意外选择。 (也是预先填充的原因)

注意:关联的分类法术语和作者元数据是相同的 (作者元数据选项是从现有分类法术语中动态提取的)。

由于某种原因-这是行不通的。 我挠头试图找出原因-我认为逻辑是正确的。 我是否想念东西/有人可以帮忙吗?

提前致谢。

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' );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM