0

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

1 Answer 1

0

Modified your code above to post to the custom post type "Bookings," set to draft, and use the user ID number as the post title. I also added a return value for the new post id so redirecting to that id or linking to it is much easier.

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'    => $user_id,
          'post_content'  => 'You can edit this or create a new story',
          'post_type'     => 'Bookings',
          'post_status'   => 'draft',
          'post_author'   => $user_id
        );

        // Insert the post into the database and return new post id
        $itinerary_id = wp_insert_post( $my_post );
        return $itinerary_id;

}
3
  • Hi Catherine Thank you for tweaking the code. I just added to the functions.php file and created a new user but it's not working. So, I did the following: Created a new user with custom user role Client, which is a clone of Author with the username 251111 Checked in the Bookings CPT and was expecting to see a draft post with title 251111 (post ID) Where do you think the problem might be? Cheers Ade
    – xbl4814
    Nov 1, 2021 at 9:57
  • @xbl4814 The custom post type name might be the issue perhaps? When you registered the custom post type with register_post_type('post_type_name', $args) did you name it 'Bookings' in the 'post_type_name' field, or is there some type of prefix? Adding the code for your custom post type and custom user role could help diagnose the issue. Nov 1, 2021 at 14:46
  • The CPT was created using Toolset :-) So, we have just listed all the CPTs and sure enough, 'Bookings' is not listed! This is the list: itinerary-days restaurant - publicly queryable. hotel - publicly queryable. the-open - publicly queryable. future-event - publicly queryable. place-to-visit - publicly queryable. I am therefore assuming the Bookings CPT is actually registered as 'the-open' as this is also the slug. I will swap out bookings for the-open and see what happens. Many thanks for your help, which is much appreciated.
    – xbl4814
    Nov 2, 2021 at 15:52

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.