Skip Navigation

[Resolved] How to limit the creation of a specific CTP to only 1 per user?

This support ticket is created 2 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.

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 6 replies, has 2 voices.

Last updated by gabrielM-7 2 years, 6 months ago.

Assisted by: Luo Yang.

Author
Posts
#2527363

Tell us what you are trying to do?
I have a CPT called 'Instructors'. I am trying to limit users to only being able to create 1 Instructor post.

I understand that I need to use Custom Code for this and add it to my functions.php file. I just need help with knowing what code exactly I need to use and how to register the Shortcode via Toolset > Settings > Front-end Content > Third-party shortcode arguments (if registering is required).

I've added the following code to my functions.php file:

function u_post_count() {
$user_post_count = count( get_posts( array(
'post_type' => 'instructor',
'author' => get_current_user_id(),
) ) );

return $user_post_count;
}

What is the next step?

Is there any documentation that you are following?
https://toolset.com/forums/topic/limit-logged-in-user-to-only-ceate-1-post/
https://toolset.com/forums/topic/limit-users-to-1-child-post-creation/

#2527559

Hello,

You can create a post view:
- Query Instructors posts
- Filter by post author is same as logged-in user
https://toolset.com/course-lesson/filtering-custom-lists-of-posts/#filtering-by-author
- Display only one item
If does not find any result, display the post form for creating new Instructors post

#2527933

Hi Luo,

Thanks for your reply.

I have done what you suggested, but it appears to have the opposite affect of what I am trying to do. It hides the 'Add a New Instructor' Form from users that haven't posted yet. And it shows the Form to users that already have posted. Maybe I've misunderstood or did not explain what I trying to do.

I need to prevent users from creating a second Instructor post. I only want them to be able to create a maximum of 1 Instructor post (per user). So I need the 'Add a New Instructor' Form to only be available to new users that have not created an Instructor post yet. And maybe a message that displays when a user has already created an Instructor post.

Also, based on this thread (https://toolset.com/forums/topic/limit-users-to-1-child-post-creation/), the recommended option is to use custom code to hook the form validation and check if the user has already posted (in order to prevent a user from building an HTTP payload to create a new Instructor post). Could you help me with this?

Many thanks.

#2528285

It seems you did not setup it correctly, you just need to try these:
1) Edit the post view, in section "Loop Editor":
- find the shortcode [wpv-items-found] ... [/wpv-items-found]
Display a message in it: You have already created an Instructor post!
- Find the shortcode [wpv-no-items-found] ... [/wpv-no-items-found]
Display the post form shortcode in it.

2) Yes, it is possible to add a validation in server side with filter hook "cred_form_validate", see our document:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

For example, see the custom PHP codes of below similar thread:
https://toolset.com/forums/topic/limit-the-number-of-posts-that-users-can-write/#post-610032

#2528939

Hi,
Thank you for your response.

How do I locate the "Loop Editor"? Is this a legacy feature? I am using the Toolset Blocks plugin.

#2529935
shortcodes.jpg

In post views created with Blocks editor, you can add two WordPress built-in shortcode blocks just above "View output block" , display above shortcodes, see my screenshot shortcodes.jpg, and display the view's shortcodes, like below:

[wpv-items-found]
Meta HTML displayed when records are found by the View
[/wpv-items-found]
[wpv-no-items-found]
No users found for this view
[/wpv-no-items-found]

More help:
https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-items-found
https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-no-items-found

#2532633

OK perfect! Thank you for providing the screenshot as it was very helpful. My issue is resolved now. Thank you so much!