Skip Navigation

[Resolved] Can I make relationship between My Custom post Type and User??

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 2 replies, has 2 voices.

Last updated by feimingH 6 years, 8 months ago.

Assigned support staff: Caridad.

Author
Posts
#155350

Hi,
I met a problem that I custom a post type for all user in the wordpress system. and the post type is a many-to-many relationship of user.
Can I make the relationship between My Custom post Type and User??

#155493

Dear Feiming,

You can't do that directly. We will be adding support for this in a future release but for now you have to rely on workarounds. You can create a custom post type that has a one-to-one relationship with users, you could call it Profile. From the you can follow the normal procedure for many to many relationships.

Lets say your Profile custom post type has a "user_id" field to connect it to the user. To create a Profile for each user you can add these lines to functions.php in your theme:

function your_function($user_login, $user) {
    $profile = get_posts( array( 'post_type' => 'profile', 'wpcf-user_id' => $user->ID ) );
    if ( empty( $profile ) ) {
      $id = wp_insert_post( array( 'post_type' => 'profile', 'post_title' => $user->display_name, 'post_status' => 'publish' ) );
      add_post_meta( $id, 'wpcf-user_id', $user->ID );
    }
}
add_action('wp_login', 'your_function', 10, 2);

Please let me know if you are satisfied with my answer and if I can help you with any other questions you might have.

Regards,
Caridad

#161595

Sorry for the delay.
Your answer realy helpful.