functions – Create WordPress User in backend, and automatically create a new post in Custom Post Type with user uame as the Title in the post

Question

We are developing a golf tour itinerary system for our clients, and we only want client with unique booking reference 221234 to see their tour itinerary, which is on a CPT titled 221234.

Is there a way that when we create/save a new user in the backend, at the same time it ‘automatically’ creates a new draft post in an existing custom post type and assigns the new user as the content author (owner) of that draft post?

The user names are all 6 digit numeric and we want the title of the draft post being created to match the user name. We can then go in an edit the draft post, knowing it is preassigned to the user account we created.

So, user 221234, has a draft post titled 221234 in the CPT and is assigned as the Autor of that new draft post.

We are designers not coders, and therefore we are developing the system with Toolset and have this conditional statement covering the master itinerary content template, which works very well for what we need.

[wpv-conditional if=" ( ( '[**wpv-current-user info='id'**]' eq '[**wpv-post-author format="meta" meta="ID"** ]' ) ) "]

It does exactly what we want. However we have to manually assign ownership and there are a lot of bookings!

So, we need to find a way to automate and create the user first, then create the draft post.

We have found this code snippet, which looks promising, but not sure how we link this to our CPT

add_action( 'user_register', 'myplugin_registration_save', 10, 1 );

function myplugin_registration_save( $user_id ) {

        $userPostsCategory = 3;

        // Create post object
        $my_post = array(
          'post_title'    => 'Sample Story' ),
          'post_content'  => 'You can edit this or create a new story',
          'post_status'   => 'publish',
          'post_author'   => user_id,
          'post_category' => array( $userPostsCategory )
        );

        // Insert the post into the database
        wp_insert_post( $my_post );

}

The CPT is called Bookings

0
kero 1 month 2021-10-28T06:55:21-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse