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