0

I have a post type that inserts email as a post. I also created users according to those emails. This is how I added my post type.

$post_array = array( 
                'post_content'  => $mail['body'], 
                'post_title'    => $mail['header']->subject,
                'post_type'     => 'my-post-type',
                'post_author'   =>  $user_name,
                'post_status'   => 'publish',
                'meta_input'    => array(   
                    'User'      => ucwords(strstr($mail['header']->fromaddress, '<',true)),
                    'From'      => preg_replace('~[<>]~', '', strstr($mail['header']->fromaddress, '<')),
                    'Email'     => $mail['header']->Date,
                    'ticket_id' => preg_replace('~[<]~','',strstr($mail['header']->message_id, '@',true)),   
                    ),
                );
                    wp_insert_post( $post_array );

This is how I added the users:

if(email_exists($email)){
    add_post_meta($post_id, 'username', $email, True);  
    add_post_meta($post_id, 'email', $user, True);
 }
    else{
         $userdata = array(
        'user_login'    => ucwords(strstr($mail['header']->fromaddress, '<',true)),
        'user_pass'     => '',
        'user_nicename' => ucwords(strstr($mail['header']->fromaddress, '<',true)),   
        'display_name'  => ucwords(strstr($mail['header']->fromaddress, '<',true)),
        'user_email'    => strstr($mail['header']->fromaddress, '<'),
        'role'          => 'support_customer',
    );
    wp_insert_user($userdata);
}

Below is the code of how I added the user role:

$result= add_role('support_customer', __(
    'Support Customer', 'faqpress'),
array(
                    'read'         => true,  // true allows this capability
                    'edit_posts'   => true,
                    'delete_posts' => true, // Use false to explicitly deny
                    'publish_posts' => true,    
                ));

Now I want the post to have the author the same as the user I just created. How do I achieve that?

5
  • Yes, All the users are created with the role support_customer and appear in my backend.
    – Rohit kc
    Commented Jan 6, 2022 at 12:11
  • No, they don't appear in the Author dropdown.
    – Rohit kc
    Commented Jan 6, 2022 at 12:15
  • I did create the Support Customer role as you suggested and it still is the same. And for the plugin you suggested doesn't work with my PHP version.
    – Rohit kc
    Commented Jan 6, 2022 at 14:53
  • I also created the user role for the users to author just to see if the users get the author to the post. But, it again does not work.
    – Rohit kc
    Commented Jan 6, 2022 at 14:59
  • Try this simple plugin - save it as wpse-401000.php and put the file in the wp-content/plugins folder, then activate the plugin, and see what the "Status" on the plugin page says.
    – Sally CJ
    Commented Jan 7, 2022 at 15:29

1 Answer 1

1

Looking at your if-else block, I see there's a $post_id variable, so if it's the ID of the post that you want to assign to the user you're creating, then you could just call wp_update_post() in your else block, i.e. right after you create the user.

So just replace the wp_insert_user($userdata); in your code with this:

$user_id = wp_insert_user($userdata);

if ( ! is_wp_error( $user_id ) ) {
    wp_update_post( array(
        'ID'          => $post_id,
        'post_author' => $user_id,
    ) );
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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