[Gelöst] Limi to create posts by user and unexpected results
Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.
Problem:
The user would like to limit posts creations for users.
Solution:
It turned out that the function "u_product_count" returned 5 for the user "croqueteria". Because the "numberposts" was not passed to get_posts, I would expect the function to return 10, from Settings->Reading, but for some reason it returns only 5.
I updated the code to introduce the "numberposts" equal to -1, to return all posts, and it worked form.
function u_product_count() {
//we create the $variable
$user_product_count = count( get_posts( array( //we get the posts
//we define the Post type
'post_type' => 'producte',
//all posts
numberposts' => -1,
//we get the current logged in author
'author' => get_current_user_id(),
) ) );
//We return the amount of posts created by user
return $user_product_count;
}
This support ticket is created vor 4 Jahren. 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.
Heute stehen keine Supporter zur Arbeit im Werkzeugsatz-Forum zur Verfügung. Sie können gern Tickets erstellen, die wir bearbeiten werden, sobald wir online sind. Vielen Dank für Ihr Verständnis.
Hello Jamal. I explain you the problem. I have a custom functions to limits pots by role. I have 2 roles gratuit (free) and premium. Gratuit can submit limited number of custom posts (40 productes - 40 wines - 2 menus). I have the following funcionts:
/**
*Count posts of given type, so each user can create post only [desired number of posts] posts
*/
function u_post_count() {
//we create the $variable
$user_post_count = count( get_posts( array( //we get the posts
//we define the Post type
'post_type' => 'negoci',
//we get the current logged in author
'author' => get_current_user_id(),
'numberposts' => -1,
) ) );
//We return the amount of posts created by user
return $user_post_count;
}
function u_product_count() {
//we create the $variable
$user_product_count = count( get_posts( array( //we get the posts
//we define the Post type
'post_type' => 'producte',
//we get the current logged in author
'author' => get_current_user_id(),
) ) );
//We return the amount of posts created by user
return $user_product_count;
}
function u_vi_count() {
//we create the $variable
$user_vi_count = count( get_posts( array( //we get the posts
//we define the Post type
'post_type' => 'vi',
//we get the current logged in author
'author' => get_current_user_id(),
) ) );
//We return the amount of posts created by user
return $user_vi_count;
}
function u_menu_count() {
//we create the $variable
$user_menu_count = count( get_posts( array( //we get the posts
//we define the Post type
'post_type' => 'menu',
//we get the current logged in author
'author' => get_current_user_id(),
) ) );
//We return the amount of posts created by user
return $user_menu_count;
}
And conditions:
[wpv-conditional if="( '[wpv-current-user info='role']' eq 'gratuit' ) AND ( u_product_count() lt '40' )"]
[cred-form form="add-producte" ]
[/wpv-conditional]
[wpv-conditional if="( '[wpv-current-user info='role']' eq 'gratuit' ) AND ( u_product_count() gt '40' )"]
No pots posar més de 40 productes
[/wpv-conditional]
My apologies for the late reply. I took the copy from your website and analyzed it locally.
It turned out that the function "u_product_count" returned 5 for the user "croqueteria". Because the "numberposts" was not passed to get_posts, I would expect the function to return 10, from Settings->Reading, but for some reason it returns only 5.
I updated the code to introduce the "numberposts" equal to -1, to return all posts, and it worked form.
function u_product_count() {
//we create the $variable
$user_product_count = count( get_posts( array( //we get the posts
//we define the Post type
'post_type' => 'producte',
//all posts
numberposts' => -1,
//we get the current logged in author
'author' => get_current_user_id(),
) ) );
//We return the amount of posts created by user
return $user_product_count;
}
As you can see in my screenshot, this is working for the free plan user on my local copy: versteckter Link
And check how the page is built on the backend: versteckter Link
I wanted to check on your website, but my user and password are not working anymore. I set your next reply as private to let you share new credentials safely. ** Make a database backup before sharing credentials. **
Hi Jamal.
With conditional blocks and your code addon it Works like a charm.
Thanks for your time and help. Your support rocks!
My issue is resolved now. Thank you!