Skip Navigation

[Resolved] User registration and connect to CPT

This support ticket is created 3 years, 7 months ago. There's a good chance that you are reading advice that it now obsolete.

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.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 3 replies, has 3 voices.

Last updated by MarcoS3712 3 years, 7 months ago.

Assisted by: Christian Cox.

Author
Posts
#1770289

I have a user registration form for new users. When they register I want to add a CPT dropdown to add the city of choice. The CPT cities is linked to cpt "news" so I would like to link cities to a user.

If that's not possible in the registration form , can it be added after registration to a user?

Thanks!
Guillaume

#1771357

Hello, it sounds like you need a way to connect a custom post type "City" to a User's profile. Toolset's post relationships feature only works to connect one post type to another post type, not to connect posts to Users. One way around this is to use a "proxy" post type that represents Users, for example, a custom post type called "Members". Each User would be the author of one Member post only. Then you can connect other post types, like Cities, to the Member post. We have more information about connecting Users and posts using a proxy post type available here: https://toolset.com/documentation/post-relationships/how-to-create-custom-searches-and-relationships-for-users/
This method does not require any custom PHP code.

Other methods to connect Users and some custom post type may require custom code. For example, you could create a generic select field in the registration form and use a View to generate the options for that select field. You could then use the cred_save_data API to store the selected post's ID in a custom field in the User's profile. We have documentation available for this API here: https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

Let me know if you have questions about either approach and I can offer more guidance.

#1771637

Thanks Christian, I connected it indeed via the cred save function. A user - cpt feature should be very welcome in the future.

#1997639

Hello, I tried to connect Author to a CPT, as 'Member Profile', in order to set a page where Author can talk about himself and to biuld a relationship with others posts. Also it would be useful to connect to taxonomies.

I've added this code in a snipped and seems to work well. What do you think? It's aa good way or is there an easier way to do it?

// Check and limit max 1 custom post type for Author
// Others posts are saved as draft
// Code source adapted from:
// https://wordpress.org/support/topic/limit-post-creation-count-by-author-or-role/
// by Howdy_McGee (@howdy_mcgee)

add_filter( 'wp_insert_post_data', function( $data, $postarr ) {
$user = wp_get_current_user();
// $user_id = get_current_user_id();
if( empty( $user) || ! in_array( 'author', (array)$user->roles ) ) {
$posts_per_period = 1; // Could be set as an updatable option
// Number of posts by this user (in current period)
$author_posts = new WP_Query( array(
'post_type' => 'member-profile',
'post_status' => 'publish',
'author' => $user_id,
'fields' => 'ids',
// activate if have to set a time period limitation as: 'monthnum' => date( 'm' ), // Whatever the current month is
) );
$author_post_count = $author_posts = $author_posts->post_count; // Add current post
if( $author_post_count > $posts_per_period ) {
$data['post_status'] = 'draft';
}
return $data;
}
}, 10, 2 );

Thanks, Marco.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.