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/
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
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.
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
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.
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
OK perfect! Thank you for providing the screenshot as it was very helpful. My issue is resolved now. Thank you so much!