WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a front end form that creates a new post from users that are not required to login. However, I need to create a new user based on the information passed in the form. As it is right now I can successfully create a new user, but in the user admin in the backend there are no posts associated with this new user (thought the posts are successful themselves).

Side note, if I delete the user and click to delete all associated posts, it will delete the post they created. I just can't figure out why it won't show their posts in the user admin...

Code as follows in my custom template file:

$new_post = array(
'post_title'    =>   $title,
'post_content'  =>   $description,
'post_category' =>   array($_POST['cat']),  // Usable for custom taxonomies too
'tags_input'    =>   array($tags),
'post_author'    => $user_id,
'post_status'   =>   'publish',           // Choose: publish, preview, future, draft, etc.
'post_type' =>   'post'  //'post',page' or use a custom post type if you want to


// Insert a user

if ( ! is_user_logged_in() ) {
        // create an account
      $user_data = array(
            'ID' => '',
            'user_pass' => wp_generate_password(),
            'user_login' => $lastname,
            'display_name' => $lastname,
            'first_name' => $firstname,
            'last_name' => $lastname,
            'role' => get_option('default_role'), // Use default role or another role, e.g. 'editor'
            'user_email' => $email
        $user_id = wp_insert_user( $user_data );
else {
    $user_id = get_current_user_id();
share|improve this question

You just need to reorder your function. Create your user before you enter your post, then the 'post_author' => $user_id field will actually have a value.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.