Skip Navigation

[Resolved] Limit Each User To Creating Only One Child Post Per Parent

This support ticket is created 5 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 2 replies, has 2 voices.

Last updated by GeneP3364 5 years ago.

Assisted by: Nigel.

Author
Posts
#1517793

Tell us what you are trying to do?

Relationship:
Parent Post is "Events"
Child Post is "RSVP"
User Role is Player

GOAL:
A Player Is Able to RSVP Any and All Events... This works...
A Player Should Only Be Able to RSVP an Event ONCE... Need Help On That.

The part I'm not getting is making the RSVP Post Count specific to the Event. A Player should be allowed to have unlimited RSVPs, but only one per Event.

Is there any documentation that you are following?
This post seemed very promising, but I'm missing something.

https://toolset.com/forums/topic/limit-the-number-of-posts-submitted-by-registered-customers-ii/

Is there a similar example that we can see?
Here's how I adapted the code from that documentation...

// Functions.php...

function rsvpevent_post_count() {

$user_post_count_rsvpevents = count(
get_posts(
array(
/* !? THIS IS PROBABLY MY MISTAKE ?! */
'post_type' => '@event-rsvp.child',
/* !? NOT SURE HOW TO SPECIFY THAT IT MUST BE CHILD ?! */
'author' => get_current_user_id(),

)
)
);

return $user_post_count_rsvpevents;
}

// CONDITIONAL IN FORM

[wpv-conditional if="( '[wpv-current-user info='role']' eq 'player' ) AND ( rsvpevent_post_count() lt 1 )"]

[cred_field field='form_submit' output='bootstrap' value='RSVP' class='uk-button']

[/wpv-conditional]

What is the link to your site?
Private

#1518589

Nigel
Supporter

Languages: English (English ) Spanish (Español )

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

Screen Shot 2020-02-20 at 13.08.48.png

Hi there

We can do this without resorting to custom code, so let's.

First up, let me clarify the workflow, because I'm not sure from your question you are doing the same, although it's what I would expect, and in any case if you have something different the principles should be the same and you can adapt what I describe.

My test site has a one-to-many relationship with project-tasks, and I set it up to have the same requirements as your scenario, i.e. any user can only add one task (child post) to a project (parent post).

I created a Form to publish tasks (which includes a parent selector).

I added the Form to a page.

I'm going to add a link to the form to publish child posts to the template for parent posts, so that when I'm looking at a Project on the front-end I see a link to add a Task.

Ordinarily I would edit the template for Projects and use the Toolset Forms button to insert the link to the add task form. If you are using the Blocks UI right now you'll struggle to find this (these are the kind of things where the Blocks UI is still catching up the existing UI). Use a Classic Block where you will see the Toolset Forms button (in icon form) which you can use to insert link to the child post.

But! Don't do that right now, because we are going to use a View to handle hiding the form link for users who already added a child post.

So, create a View to display the child posts. You will need to insert two Query Filters to limit which child posts are returned. We want the View to return child posts which belong to the current parent post that are authored by the current user.

So the first is a relationship filter that will specify the parent according to where this View is displayed, and the second specifies the post author (see screenshot; you'll find something similar in the sidebar if you are using the Blocks UI).

Now to set up the output section.

If there are any such posts already then we don't want to output anything. If there are no such posts (the user didn't add a child post yet) then it's okay to output the link to the form.

So I don't output anything in the View loop, and instead in the no items found section I use the Toolset Forms button to insert the shortcode that generates the link to child post form.

My output section then looks like this:

[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
		<wpv-loop>
		</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
		[cred_child_link_form form='38' parent_id='-1' text='Create new task' target='_self']
	[/wpv-no-items-found]
[wpv-layout-end]

You can do this with the Blocks editor for Views, but you will find it easier with the Classic editor because you can't use the Toolset Forms button to generate the no-items-found content, it currently only accepts text.

Lastly, to simplify the formatting, you can check the option "Disable the wrapping DIV around the View".

So I return to edit the template for the parent Projects, and insert this View where I want the edit link to appear, and it only will appear to users who have not already added a child post to that parent.

That's what's required, I realise you may need more details for some of the steps.

If so, let me know where you get stuck.

#1520005

Nigel! Dude! Not only was this a perfect and simple solution it broadened my understanding of views and what can be accomplished with them. Very creative solution sir! Thank you, my issue is now resolved.