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?
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]
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.
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.
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
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?
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?