我希望为每个至少拥有作者凭据的用户自动为自定义帖子类型(jt_cpt_team
)创建帖子。
首先,它需要为管理员创建的每个新用户执行此操作。如果有一种简单的方法可以为每个现有作者生成一个帖子(~30)那么这很好,但如果需要,我不介意手工完成这些。
我猜这或多或少是我需要的东西 - 努力让它发挥作用......
class team_functions {
public function __construct() {
add_action('user_register', array($this, 'create_authors_page'));
}
public function create_authors_page($user_id) {
$the_user = get_userdata($user_id);
$new_user_name = $the_user->user_login;
$my_post = array();
$my_post['post_title'] = $new_user_name;
$my_post['post_type'] = 'jt_cpt_team';
$my_post['post_content'] = '';
$my_post['post_status'] = 'publish';
$my_post['post_theme'] = 'user-profile';
wp_insert_post( $my_post );
}
}
此外,如果有办法将作者的电子邮件添加为custom_field
,那将是非常棒的。
提前感谢您的所有帮助:)
答案 0 :(得分:1)
这样的事情应该有效:
public function create_authors_page( $user_id ) {
$the_user = get_userdata( $user_id );
$new_user_name = $the_user->user_login;
$PostSlug = $user_id;
$PostGuid = home_url() . "/" . $PostSlug;
$my_post = array( 'post_title' => $new_user_name,
'post_type' => 'jt_cpt_team',
'post_content' => '',
'post_status' => 'publish',
'post_theme' => 'user-profile',
'guid' => $PostGuid );
$NewPostID = wp_insert_post( $my_post ); // Second parameter defaults to FALSE to return 0 instead of wp_error.
// To answer your comment, adding these lines of code before the return should do it (Have not tested it though):
$Key = $new_user_name; // Name of the user is the custom field KEY
$Value = $user_id; // User ID is the custom field VALUE
update_post_meta( $NewPostID, $Key, $Value );
return $NewPostID;
}