在创建新帖子之前强制选择类别

时间:2018-05-23 作者:Duy Hữu

我看到这个帖子了https://wordpress.stackexchange.com/a/14405/144156

代码工作正常,但我想更改category=我的自定义分类法“chuyen muc”,有人帮我,谢谢。

1 个回复
最合适的回答,由SO网友:Aurovrata 整理而成

使用\'save_post\' 钩子以编程方式在创建新帖子时输入它,然后使用wp_set_post_terms() function 要分配您的学期,

add_action(\'save_post\',\'set_post_default_category\', 10,3);
function set_post_default_category($post_id, $post, $update){
  if($update) return; //only want to set if this is a new post!
  if(\'post\' !== $post->post_type) return;  //only set for post_type = post!
  $term = get_term_by(\'slug\', \'my-custom-term\', \'category\');//get the default term using the slug, its more portable!
  wp_set_post_terms( $post_id, $term->term_id, \'category\', true );
}

结束

相关推荐

Inserting two categories

我试图在一篇文章中同时定义两个类别。请告诉我哪里做错了。\'post_author\' => $user_id, \'post_status\' => \'pending\', \'post_category\' => array($_POST[\'cat\'],$_POST[\'joblocation\']) //\'taxonomy\' =>