Skip Navigation

[Resolved] limit the count of submission form one post per role

This support ticket is created 6 years, 11 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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: Asia/Hong_Kong (GMT+08:00)

Author
Posts
#594534

I am trying to limit the count of submission form one post per user by specific role, and have read the article of https://toolset.com/forums/topic/limiting-cred-form-submission-to-one-post/

I guess something I set up wrong, so that it is still not working

add_filter('cred_form_validate','my_validation',10,2);
function my_validation($field_data, $form_data)
{
    //field data are field values and errors
    list($fields,$errors)=$field_data;
   
    //uncomment this if you want to print the field values
    //print_r($fields);
   
    //validate if specific form
    if ($form_data['id']==622) ****************** is it the cred form using for generating a post right?
    {       
            $user = get_current_user_id();
            $user_post_count = count_user_posts( $user , 'house' ); **********what does House mean?
            $user_data = get_userdata($user);
            $user_role = $user_data->roles;
             
        if ( $user_role[0] == 'consultant' && $user_post_count > 0)
        {
            //set error message for my_field
            $errors['wpcf-user-validation']='Only 1 post per user is allowed';  
        }
   
        if ( $user_role[0] == 'business' && $user_post_count > 0) ************business,  just replace it with the name of role which I want to limit?
        {
            //set error message for my_field
            $errors['wpcf-user-validation']='Only 1 post per user is allowed';
        }
   
        //check if featured image exists
      
    }
   
    //return result
    return array($fields,$errors);
} 

please check the question I marked with **** in code box. do I need to pay attention to something more? Thank you so much!

#594535

Can I suppose that "House" is post type name or slug? which one is correct? second, how can I disable the submit button? after specific user submitted one post? Thanks!

#594602

Dear Jeffrey,

Q1) what does House mean?
Yes, you are right, "House" is post type slug, see wordpress document:
https://codex.wordpress.org/Function_Reference/count_user_posts
The second parameter in function count_user_posts($userid , $post_type) is:
$post_type
(string) (optional) Post type to count the number of posts for.

Q2) is it the cred form using for generating a post right?
Yes, it is specific CRED form ID

Q3) business, just replace it with the name of role which I want to limit?
Yes, you are right.