Skip Navigation

[Resolved] I need help with limitting posts from a user role

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

Problem:

Create a custom function to count user's posts, and use it in the [wpv-conditional] shortcode.

Solution:

1) Edit the file functions.php, add below codes:

function my_u_post_content() {
$user_post_count = count( get_posts( array(
'post_type' => 'anuncio-gratuito',
'author' => get_current_user_id(),
) ) );
return $user_post_count;
}

2) Dashboard-> Toolset-> Settings-> Conteúdo de front-end
in option "Funções dentro de avaliações condicionais", add above function name "my_u_post_content"

3) Create a page to test the result:

[wpv-conditional if="( my_u_post_content() lt '20' )" ]
<label> test </label>
[/wpv-conditional]

[wpv-conditional if="( my_u_post_content() gt '20')" ]
<label> test 2</label>
[/wpv-conditional]

Relevant Documentation:

100% of people find this useful.

This support ticket is created 6 years, 7 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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: Asia/Hong_Kong (GMT+08:00)

This topic contains 7 replies, has 2 voices.

Last updated by selmoK 6 years, 7 months ago.

Assisted by: Luo Yang.

Author
Posts
#646230

Hi,

I am following the ticket https://toolset.com/forums/topic/limit-the-number-of-posts-submitted-by-registered-customers/#post-368894

1) I have included the custom function on my functions.php:
function u_post_count() {
$user_post_count = count( get_posts( array(
'post_type' => 'Anúncios gratuitos',
'author' => get_current_user_id(),
) ) );
return $user_post_count;
}

2) Registered manually u_post_count on settings, front-end, Functions inside conditional evaluations

3) I put on the cred form the conditional:
[wpv-conditional if="( u_post_content() lt '20' )" ]
<label> test </label>
[/wpv-conditional]

[wpv-conditional if="( u_post_content() gt '20')" ]
<label> test 2</label>
[/wpv-conditional]

I have 11 posts from this type. But nothing happens, it seems that u_post_content() is not called.

Can you help me, please?

#646300

Hello,

You will need to use post type slug in the parameter "post_type" of WordPress function get_posts(), for example, the post type "Anúncios gratuitos" is using slug "anuncios-gratuitos", you can modify PHP codes, this line from:

'post_type' => 'Anúncios gratuitos',

To:
'post_type' => 'anuncios-gratuitos',

More help:
https://codex.wordpress.org/Class_Reference/WP_Query#Type_Parameters

#646414

Hi Luo,

I tried to change the post_type but it still didn´t work.

#646565

Since it is a custom codes problem, please provide a test with the same problem, fill below private detail box with login details and FTP access, also point out the problem page URL, and where I can edit you PHP codes, I need a live website to test and debug, thanks

#651000

Thanks for the details, I can login into your website, and I have tried to create a page and tested the codes you mentioned above, I can see the problem, it does not work.

And it is abnormal, in case it is a compatibility problem, please try this:
1) deactivate other plugins and switch to wordpress default theme, copy those PHP codes into wordpress default theme folder, file functions.php, and test again
2) If the problem still persists, please provide a database dump file (ZIP file) of your website in below private detail box, also point out the problem page URL, I need to test and debug it in my localhost, thanks

#651192

Hi Luo,

I tried to change the function name from u_post_count to quantpost (removing _ from name) that is being called and returning a value. The problem now that it is returning zero value. Could you check again, please?

Thanks in advance

#651824

As I mentioned above in my first answer:
https://toolset.com/forums/topic/i-need-help-with-limitting-posts-from-a-user-role/#post-646300
You will need to use post type slug in the parameter "post_type" of WordPress function get_posts(),

You can get the post type slug by editing the post type 'Anúncios gratuitos':
hidden link
find it in the option "Slug (obrigatório)"

And I have checked in your theme file functions.php, there isn't any function with name "quantpost", so I have created another new function with new name "my_u_post_count" in your website, and have done below modification in your website:
1) Edit the file functions.php, add below codes:

function my_u_post_content() {
$user_post_count = count( get_posts( array(
'post_type' => 'anuncio-gratuito',
'author' => get_current_user_id(),
) ) );
return $user_post_count;
}

As you can see, I have only changed the function name

2) Dashboard-> Toolset-> Settings-> Conteúdo de front-end
in option "Funções dentro de avaliações condicionais", add above function name "my_u_post_content"

3) Create a page to test the result:
hidden link

Test it in front-end, it works fine. can you confirm it?

#653294

Dear Luo,

It worked fine! Thank you very much!