1

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();
}

1 Answer 1

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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