Skip Navigation

[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;
}

Relevant Documentation:

https://developer.wordpress.org/reference/functions/get_posts/

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.

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: Africa/Casablanca (GMT+01:00)

Dieses Thema enthält 7 Antworten, hat 3 Stimmen.

Zuletzt aktualisiert von David Gimenez vor 4 Jahren.

Assistiert von: Jamal.

Author
Artikel
#1831039

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]

The first works fine but second not.

#1831085
#1831097

Hi again Jamal... i need a private message for credentials.

Thank U

#1833329

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;
}

https://developer.wordpress.org/reference/functions/get_posts/

I'll let you update the code of all the functions. Let me know if you have any questions.

#1837839

Hi Jamal, sorry for my delay.

I updated the funcion and I tested it and it doesn't work

This code doesn't output any message.

[wpv-conditional if="( '[wpv-current-user info='role']' eq 'gratuit' ) AND ( u_product_count() lt '40' ) )"  <p>Tens menys de 40 productes</p>
[cred-form form="add-producte" ]
[/wpv-conditional]

[wpv-conditional if="( '[wpv-current-user info='role']' eq 'gratuit' ) AND ( u_product_count() gt '40' ) )" ]
<p>Tens més de 40 productes</p>
[/wpv-conditional]

u_product_count doesnt't work.

Any idea....

#1837847

Jamal... wait. Let me check...
Thanks!

#1840301

Nigel
Supporter

Sprachen: Englisch (English ) Spanisch (Español )

Zeitzone: Europe/London (GMT+00:00)

Jamal is off on vacation but will be back soon, if you have an update for him when he returns please share it.

#1842031

Hello,

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

#1844891

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!