Skip Navigation

[Resolved] Limit users to create only 1 post type. Create new user and new post.

This thread is resolved. Here is a description of the problem and solution.

Problem:
How can I limit an User to create only one Post with a CRED Form?

Solution:
https://toolset.com/forums/topic/limit-users-to-create-only-1-post-type-create-new-user-and-new-post/#post-588796

This support ticket is created 7 years 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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Tagged: 

This topic contains 2 replies, has 2 voices.

Last updated by tiagoS-3 7 years ago.

Assisted by: Beda.

Author
Posts
#588780

I need to associate taxonomy to users. So, i create a CPT "Users Preferences" to intermediate (https://toolset.com/forums/topic/taxonomy-to-wp-users-and-view-filter/).

Question 1 - Is it possible to use CRED new user form and also create the "Use Preference"? Whats the best way to create a new user and CPT "User Preference" with this user like author?
Q2 - How can i limit users to create only 1 unique post "User Preference"?
Q3 - How can i edit this link for this unique post to edit with CRED?

#588796

Is it possible to use CRED new user form and also create the "Use Preference"? Whats the best way to create a new user and CPT "User Preference" with this user like author?

Yes, sure. You need to hook into the Form Submit action, get the current user data or newly registered user data from the $_POST of the CRED Form, and then use this User data to populate the Posts' author meta.
The post will also be inserted during the same action.

To hook your Custom Code to CRED you use:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

Then, within this hook you get the User ID with get_user_by(), using the User's email or username:
https://developer.wordpress.org/reference/functions/get_user_by/

After, you insert a new post (still within the CRED Save Data action) and pass the user ID as it's author:
https://developer.wordpress.org/reference/functions/wp_insert_post/

How can i limit users to create only 1 unique post "User Preference"?

Toolset does not support this out of the box.
Also, since you create your Post with an User Form and CRED API, the user will never be able to reach that form again (since the user now exists you do not expect him register again, right?)
Hence the risk of more than one post each user is equal 0.
If you want, you can as example wrap the link that leads to the User Register form in a HTML condition that uses a custom ShortCode or function, which checks if the user already has posts

Here an example for that:

/**
 *Count posts of given type, so each user can create post only once
 */
function u_post_count() {
    $user_post_count = count( get_posts( array( 
    'post_type' => 'your_post_type', 
    'author'    => get_current_user_id(), 
) ) );
        
    return $user_post_count;
}

==> It will count all Posts of the current logged in user.
==> Make sure to define the Post type in 'post_type'
==> It returns a numeric count.

This can then be used in a HTML condition to hide or show things:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/

How can i edit this link for this unique post to edit with CRED?

You create a CRED Edit form for this Post Type and add it to a Content Template.
Then you link to that Content Template with the Toolset ShortCode for CRED Edit Links:
https://toolset.com/documentation/user-guides/displaying-cred-editing-forms/

#588876

Works fine!

About question 1, I used this:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
    if ($form_data['id']==19)
    {
	  
$my_post = array(
  'post_title'    => $post_id ,
  'post_content'  => 'This is the content',
  'post_status'   => 'publish',
  'post_author'   => $post_id,
  'post_type' => 'preferences'
);
   
wp_insert_post( $my_post );
    }
}

Thanks Beda!