Skip Navigation

[Resolved] Restrict the number of posts a user can submit per "Post Type"

This support ticket is created 3 years 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
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 15 replies, has 2 voices.

Last updated by philipC-4 3 years ago.

Assisted by: Shane.

Author
Posts
#2241703
Screen-Shot-2021-12-10-at-9.16.37-AM.jpg

Tell us what you are trying to do?

Hello,

We would like to restrict the number of posts a user can submit to 10 per "Post Type".

Your support specialist Shane previously helped us with this code, however, it is not functioning.

You will have to log into the website in order to see the following pages, please let me know when it's safe to share access credentials.

Custom Code Name: current_user_post_count

Custom Code provided by support specialist Shane:
function current_user_post_count() {

$current_user_id = get_current_user_id();
$posts_count = count_user_posts($current_user_id, 'car');
return $posts_count;

}
add_shortcode( 'current_user_post_count', 'current_user_post_count' );

The post form the code has been applied to:
hidden link

Archive Page:
hidden link
(as you will see when you log in the 11th submission is showing up.)

#2241733

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Philip,

Thank you for getting in touch.

This code was written to count the number of posts the user has.

Does the code not return the number of post the user currently have? In the screenshot I can see that it is set to 'car' for the post type. Did you change this to the slug of your post type that you want to check ?

Please let me know.
Thanks,
Shane

#2241935

Hello Shane,

Thank you for your response. perhaps this is not the correct code for what we are trying to achieve? Yes, I have tried renaming "car" to reflect the form we are testing.

Please advise, we appreciate your help.

#2241937

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Philip,

How this code is meant to work with our Conditional block. This shortcode will return the number of published posts the user has under a particular custom post type.

From there you can check with our conditional code if that number is greater than 10 and hide the frontend form if it is.

Essentially the frontend form will only display if the user has less posts than what is being checked by the conditional. I hope this clears up the issue for you.

Thanks,
Shane

#2241945
Screen-Shot-2021-12-10-at-2.36.15-PM.jpg
Screen-Shot-2021-12-10-at-2.36.24-PM.jpg
Screen-Shot-2021-12-10-at-2.36.34-PM.jpg

Hello Shane,

Yes sir, that makes sense. However, the form is still visible on the front end. The post form name is diy-structural-systems-form. Will you take a look at the screenshots and see if we have it set up correctly?

<?php
/**
* New custom code snippet (replace this with snippet description).
*/

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.

function current_user_post_count() {

$current_user_id = get_current_user_id();
$posts_count = count_user_posts($current_user_id, 'diy_structural_systems_form');
return $posts_count;

}
add_shortcode( 'current_user_post_count', 'current_user_post_count' );

#2241971

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Philip,

Would you mind allowing me to have admin access to the website so that I can have a more detailed look at this for you ?

Please where applicable please provide me with a link to an example page where I can see the issue.

I've enabled the private fields for your next response.

Thanks,
Shane

#2243573

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Phillip,

The problem was that you were using the wrong post type slug to get the information for the current user. The correct slug should be 'property-listing' but you had 'diy_structural_systems_form'

Please let me know if this helps.
Thanks,
Shane

#2243665

Hello Shane,

That did the trick! THANK YOU SO MUCH.

If we wish to add the same code to the other forms, what additional code must be added to the existing code? We applied the same code to "appliances" and received the (There has been a critical error on this website.)

Here is the code we tried to add:

<?php
/**
* New custom code snippet (replace this with snippet description).
*/

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.

function current_user_post_count() {

$current_user_id = get_current_user_id();
$posts_count = count_user_posts($current_user_id, 'appliance');
return $posts_count;

}
add_shortcode( 'current_user_post_count', 'current_user_post_count' );

#2243733

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Philip,

We will need to modify the code to be dynamic so that it accepts the post type slug as an attribute.

Change your code to.

function current_user_post_count($atts) {
// Attributes
	$atts = shortcode_atts(
		array(
			'post_type' => '',
		),
		$atts
	);
$current_user_id = get_current_user_id();
$posts_count = count_user_posts($current_user_id, $atts['post_type']);
return $posts_count;

}

Then go to your conditional block and modify the code accordingly like below.

  ( ( '[current_user_post_count post_type='my-post-type-slug' ]' lte '10' ) ) 

This can be found in the advanced editor section of your conditional statement.

Please let me know if this helps.
Thanks,
Shane

#2243773

Hello Shane we have updated & applied the code to property-listing (Structural Systems) and Appliances.

Structural Systems has 9 current postings, the form is not visible for the 10th posting
Post Form:
hidden link

Archive:
hidden link

Appliances has 8 current postings, the form is not visible for the 9th & 10th posting
Post Form:
hidden link

Archive:
hidden link

Your help is much appreciated!

#2243777

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Philip,

In the conditional statement you have my exact example

( ( '[current_user_post_count post_type='my-post-type-slug' ]' lte '10' ) ) 

You're to replace 'my-post-type-slug' with the actual slug of the custom post type that you're checking against. I went ahead and made these changes for you.

Please let me know if everything is ok now.

Thanks,
Shane

#2243831

Hello Shane,

Thank you for all your help. This makes complete sense.

The form still allowed an 11th post. We would like to only allow 10 posts in total to post.
hidden link

Does the code need to be adjusted to 9?

#2244365

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Philip,

This is an archive page so it will show all the posts that were created.

What is happening here is that its showing the posts that were created before the conditional was fixed.

What you need to do is to delete the 11th post. However given that its an archive it is going to show all the posts that were created even by other authors.

Thanks,
Shane

#2244425

Hello Shane,

Is there any way to only show the logged-in authors' posts?

#2244501

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Philip,

I've double checked again and your archive is already showing the posts of the current user so there isn't anything else to do here.

No further intervention will be required on my part to display the posts of the current user on the archive.

Thanks,
Shane