是否根据帖子作者元数据自动填充帖子分类数据?

时间:2014-08-31 作者:Caroline

我正在尝试使用作者元数据在给定作者的帖子上自动填充自定义分类法。目前,分类法在post页面上可见,但我打算在前端隐藏它,以避免意外的用户选择。(也是人口前期的原因)

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

出于某种原因,这是行不通的。我挠头想弄明白为什么——我认为逻辑是正确的。我错过什么了吗/有人能帮忙吗?

提前谢谢。

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

1 个回复
SO网友:Caroline

为任何想知道的人找到了答案:

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

结束

相关推荐

GET_POSTS()返回所有帖子,而不是用‘POST_Author’=>指定的帖子

我想为自定义帖子类型类别检索一个包含特定用户帖子的数组。然而,get\\u posts($arg)返回此自定义post类型的所有post,尽管我指定了post\\u作者<?php $pages = get_posts(array(\'post_type\' => \'profile\',\'post_author\' => $post->post_author)); ?> <?php var_dump ($pages); ?> 在本例