Skip Navigation

[Resolved] User registration and connect to CPT

This support ticket is created 3 years, 8 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
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+01:00)

This topic contains 8 replies, has 2 voices.

Last updated by MarcoS3712 3 years, 8 months ago.

Assisted by: Jamal.

Author
Posts
#1997641

Hello,
I've read this resolved post: https://toolset.com/forums/topic/user-registration-and-connect-to-cpt/

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

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

// 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.

#1997803

Hello and thank you for contacting the Toolset support.

Well, from what I can see too far, I believe that the code has an error. It needs a closing } before "return $data;". And I don't think it will serve its purpose. And the use of "wp_insert_post_data" is probably not a good idea unless you are letting users create their posts from the backend or using a 3rd party plugin. If you are using a Toolset form, I would suggest a similar code to this reply https://toolset.com/forums/topic/limit-the-number-of-posts-that-users-can-write/#post-610032
It uses Toolset form API instead of WordPress's https://toolset.com/documentation/programmer-reference/cred-api/

It's worth mentioning another technique that won't need any custom code, we published an article about it https://toolset.com/course-lesson/how-to-create-custom-searches-and-relationships-for-users/

#1998087

Thanks Jamal fro your suggestion.
Code works well and yes, users create their posts from the backend.
In each case I try to implement the technique your report in last line.
Marco.

#2001785

Hello Marco, and my apologies for the late reply, but I do not work on Wednesdays and Thursdays.

The technique I suggested is when you offer users to create posts from the frontend, using Toolset Forms.

If you are allowing users to create posts from the backend, the code that you are using is setting the post's status to draft when the user has more than one post.

So, I would say, that's a good way to implement the feature.

Let me know if you have any further questions.

#2005151
Author Profile.png

Thanks Jamal, and sorry for my delay.

I've set it on frontend with Toolset Forms.
I've created a new CPT "MEMBER PROFILE" in order to manage and show custom Author informations.

These are steps I followed:

  • With Toolset Forms I created "Insert Author Form Profile";
  • I create a new page "Insert Author Profile" - restricted with ToolSet Access Control - with a view (Insert Author View), and inside these I put post form with view and Post Author filter (the same as the logged in user);
  • A leave in each case the code above for CPT in order to be sure that only one post is published;
  • Here attached a map.

    Then I've set the same in order to let Author to update his post (with pending review).

    Now, the question is: how may I show Author info opening from a view?

    I have a CPT view with post title, author name, etc.
    Have I to create another view from CPT MEMBER PROFILE with only Tile View wiht hyperlink and insert in CPT view? Or simply I have to insert a shortcode?

    Thanks for your suggestion,
    Marco.

    #2005237

    Sorry, I am not sure to really understand your last questions. Have you been able to limit users to create one MEMBER PROFILE by user? Or is this still not working?
    If you are using a Toolset Form, why not use custom code for form validation. Hook into the cred_form_validate filter.
    https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

    I assume that you are setting the post as draft, you should reflect that in your query by querying multiple statuses (publish, pending, draft).

    Would you like to allow me temporary access to check what you have done so far? Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **

    #2005473
    Author Link to Profile.png

    Thanks Jamal, I can create ONE Member Profile and it works well.
    Now I have a "Author Profile" page with inside an Author Profile Template.

    Now, I would like to open this page from another CPT posted from the Author.
    I have a view as in the attachment with Author name.
    How may I open "Author Profile" from a link (for example Author Name in the red box).
    With a shortcode? May I insert in the view a link from CPT "MEMBER PROFILE"? ... Or what else?

    Thanks, Marco.

    New threads created by Jamal and linked to this one are listed below:

    https://toolset.com/forums/topic/link-to-author-profile-page/

    #2005513

    Awesome! I am glad I could help.

    However, for support rules, we are able to handle only one issue at a time. This helps us to bring you a better service and also helps other users to find all the information here exposed. For that reason, I have to kindly ask you to mark this ticket as closed.

    I have created another ticket to answer your last question, I'll be posting my reply there.

    #2005535

    Issue has been opened in a new ticket.