Skip Navigation

[Resolved] Conditional Output for Form – limit post count

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

Problem:
Conditional Output for Form based on number of posts and limit post count for Toolset forms
Solution:
You can find the proposed solution in this case with the following reply:
https://toolset.com/forums/topic/conditional-output-for-form/#post-1345769

Relevant Documentation:
=> https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-custom-functions-in-conditions/

This support ticket is created 5 years, 4 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 12 replies, has 2 voices.

Last updated by FelipeP5703 5 years, 3 months ago.

Assisted by: Minesh.

Author
Posts
#1343563

Tell us what you are trying to do?
I'm trying to only show a form if the number of custom posts are less than assigned.

I created a custom field named total-de-anuncios-pagos (slug) which is a number custom field. This will serve as total number of custom posts we allow, so for this example, we set as 5. Therefore for this author, we only allow 5 custom posts.

Now I need to create a conditional output that would compare this custom field to the total number of custom posts (including publish, drafted, and pending), and the form will only show if there are 4 or less custom posts by this author.

I'm not sure if I add this conditional output to the form itself or do I create a view. And how do I count the total number of posts by that author?

Step by step would really help!

Thank you!

#1343647

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

You need to use custom function to count number of posts.
1) You should add the followind code to your current theme's functions.php file

/**
 *Count posts of given type, so each user can create post only [desired number of posts] posts
 */
function u_post_count() {
    
    //we create a $variable that holds the value of posts of this user
    //we use the count() function <em><u>hidden link</u></em>
    //we use a get_posts to get the posts of given type, and get_current_user_id() to get the current logged in user 
    //https://codex.wordpress.org/Template_Tags/get_posts
    //https://codex.wordpress.org/Function_Reference/get_current_user_id
    
    //we create the $variable
    $user_post_count = count( get_posts( array( 
        
    //we define the Post type 
    'post_type' => 'your-post-type', 
    'post_status' => 'any',
    
    //we get the current logged in author
    'author'    => get_current_user_id(), 
) ) ); 
         
    //We return the amount of posts created by user  
    return $user_post_count ;
}

Where:
-- Above function will return a total number of posts created by logged in user with CPT you define.
-- Please change your-post-type
-- Please do not forget to register this function inside:
=> Toolset > Settings > Front-end Content > Functions inside conditional evaluations.

2)
Use the following conditional statement to check post count created by logged in user and which specific role is assigned to user.

[wpv-conditional if="( u_post_count() lt '[types field='total-de-anuncios-pagos'][/types]' )"]
  
 // add your Toolset form here 
  
[/wpv-conditional]

More info:
=> https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-custom-functions-in-conditions/

#1344157

I followed the steps you gave with no luck... here is a video I made showing it - hidden link

#1344709

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

The only difference is you are using Types user field.

Can you please try to use the following conditional statement:

[wpv-conditional if="( u_post_count() lt '[wpv-user field="wpcf-total-de-anuncios-pagos"]' )"]
   
 // add your Toolset form here 
   
[/wpv-conditional]

More info:
=> https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-user

It should work now, can you please confirm.

#1344943

Still not working - hidden link

#1344947

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please share problem URL and access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#1345323
u-post-count code3.jpg
u-post-count code2.jpg
u-post-count code.jpg

Minesh, I don't think your code u_post_count is working.

First of all, I added the code to the Custom Code in Settings - Toolset. Then I just wanted to see if it was producing a number, so I added to the Page Meus Anuncios at the bottom using Elementor Shortcode widget. But it's not producing any number.

See images

#1345769

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - I've added the following shortcode to your u_post_count custom code:

function func_count_totat_posts_by_author() {
    
     
    //we create the $variable
    $user_post_count = count( get_posts( array( 
         
    //we define the Post type 
    'post_type' => 'anuncio', 
    'post_status' => 'any',
     
    //we get the current logged in author
    'author'    => get_current_user_id(), 
) ) ); 
          
    //We return the amount of posts created by user  
    return $user_post_count ;
}
add_shortcode('totat_post_count','func_count_totat_posts_by_author');

Then, when I checked your custom user field "numero-de-anuncios-pagos", you set default value as 2. But please note that the default value will not be saved with field "numero-de-anuncios-pagos" until you save your profile.
=> hidden link

so, after login, I visited my profile and just resaved it:
=> hidden link

Now, I've added the following shortcode to the following page:
=> hidden link

[wpv-conditional if="( '[totat_post_count]' lt '[types usermeta='numero-de-anuncios-pagos' output='raw' current_user='true'][/types]' )"]
    
 // add your Toolset form here 
  condition is true 
    
[/wpv-conditional]

And when I checked the page on frontend - I see now conditional statement works as expected:
=> hidden link

#1346145

But please note that the default value will not be saved with field "numero-de-anuncios-pagos" until you save your profile.

I'm not sure if I understand you correctly. I added the default number because everyone who signs up can only add one custom post, so if they decided to add more, they will need to contact us, then we would change that custom field to 2 or 3, etc. Get it?

So I don't understand what did you mean by "save your profile."

How can I make sure when the User signs up, they get a default value in that custom field?

#1346157

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I'm not sure if I understand you correctly. I added the default number because everyone who signs up can only add one custom post, so if they decided to add more, they will need to contact us, then we would change that custom field to 2 or 3, etc. Get it?
=> Yes, I got it.

How can I make sure when the User signs up, they get a default value in that custom field?
=> how you are allowing the user to sign up - using Toolset form?

#1346169

Yes I'm using Toolset Form

Here is the link to register - hidden link

#1346179

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

To set the default value to 2 for every user, you need to add the following code to your current theme's functions.php file:

 
add_action('cred_save_data','func_set_default_value_forusers',10,2);
function func_set_default_value_forusers($post_id,$form_data) {

    if ($form_data['id']==128) {
            $limit_user_posts = 2;
           update_user_meta($post_id, 'wpcf-numero-de-anuncios-pagos', $limit_user_posts);
     }
}

Now, you should create a new user and check everything should work now. With every newly created user it will assign 2 as post limit.

#1346363

It is working! Thank you Minesh! My issue is resolved now. Thank you!