我有代码来创建新帐户和新帖子。所有这一切都正常,但我希望创建的新帐户成为新帖子的作者。
我该怎么做?
/ *创建帐户* /
if (isset($_POST['task']) && $_POST['task'] == 'register')
{
$psw = $wpdb->escape(trim($_POST['psw']));
$email = $wpdb->escape(trim($_POST['email']));
if (email_exists($email))
{
$err = "Email addres exist";
}
else
{
$user_id = wp_insert_user(array(
'user_pass' => apply_filters('pre_user_user_pass', $psw) ,
'user_login' => apply_filters('pre_user_user_login', $email),
'user_email' => apply_filters('pre_user_user_email', $email),
'role' => 'author'
));
}
...
<form method="post" id="form_anunturi" enctype="multipart/form-data">
<input type="text" name="title"/>
<textarea name="desc"></textarea>
<input type="text" name="email"/>
<input type="text" name="psw"/>
<input type="hidden" name="task" value="register"/>
<input type="submit" value="Submit"/>
</form>
更新
$date = array(
'post_title' => $title,
'post_content' => $desc,
'post_status' => 'publish',
'post_category' => array(
$categorie
) ,
'post_author' => $user_ID,
);
$post_id = wp_insert_post($date);
答案 0 :(得分:2)
如果成功, wp_insert_user()将返回新创建的用户的userID。因此,如果用户创建成功,则只需调用wp_insert_post,将新ID作为 post_author 传递:
$user_id = wp_insert_user(array(
'user_pass' => apply_filters('pre_user_user_pass', $psw) ,
'user_login' => apply_filters('pre_user_user_login', $email),
'user_email' => apply_filters('pre_user_user_email', $email),
'role' => 'author'
));
$my_post = array(
'post_title' => 'My post',
'post_content' => 'This is my post.',
'post_status' => 'publish',
'post_author' => $user_id
);
wp_insert_post( $my_post );