Skip Navigation

[Resolved] Show or hide view results based on the value of a child custom field

This support ticket is created 4 years, 6 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 6 replies, has 2 voices.

Last updated by patrickG 4 years, 6 months ago.

Assisted by: Christian Cox.

Author
Posts
#1705829
Screen Shot 2020-07-13 at 12.22.12 PM.png

Hello,

I am currently working inside a view called Client Campaigns and I am trying to filter on campaigns that have a child post type.

1: Campaigns have a relationship to the standard WordPress Post type of one to many. The campaign type is the parent to the post type.

2: Each client can view all of the campaigns but they can only create one post per campaign.

3: I want to hide all campaigns that the client has created a post for so they do now appear in their view results anymore. The view logic would be something like: If ( currentUserId == childPostAuthorId ) then do not show the post.

4: I would like to be able to toggle the campaigns with posts to show and hide the results.

#1705933

I want to hide all campaigns that the client has created a post for so they do now appear in their view results anymore. The view logic would be something like: If ( currentUserId == childPostAuthorId ) then do not show the post.
Hello, there's no conditional logic for filter exactly like this built into the system. Instead, what you can do is use a View of Posts to determine whether or not the Campaign information should be displayed. Let me explain.
- Remove the Campaign information from the Loop of the Campaign View. (paste it in a text editor for use later if you'd like, see below)
- Instead, insert a nested View of Posts with a post relationship filter and a post author filter to determine whether or not a related post exists by the current User as post author.
- In the loop of the Posts View, skip the Loop Wizard and do not include anything.
- Instead, insert the current Campaign post information in the "No results found" section. Paste it in from the Campaign View if you'd like. To access the post ID of the Campaign post, you can pass the current Campaign post ID into the View of Posts using a shortcode attribute, like this:

[wpv-view name="Your nested View of Posts" campaign="[wpv-post-id]"]

Now you have access to the current campaign ID inside the View of Posts using the attribute shortcode:

[wpv-attribute name='campaign']

Inside the Posts View, you can use the wpv-attribute shortcode in the "item" attribute of another Views shortcode or Types field shortcode to display information about the Campaign post:

Post link for Campaign: [wpv-post-link item="[wpv-attribute name='campaign']"]
Custom field in Campaign: [types field="campaign-start-date" format="Y/m/d" style="calendar"  output="normal" item="[wpv-attribute name='campaign']"][/types]

It's like using the View as a conditional, displaying nothing if the conditional is true but displaying the parent post information if it's false. There is a similar use case described in the first FAQ for this document: https://toolset.com/documentation/post-relationships/how-to-create-custom-searches-and-relationships-for-users/
Your View of Posts will be nested inside a View of Campaigns, and will also include a post relationship filter where the parent Campaign is set by the current post in the loop.

Let me know if you have questions about setting up this nested View structure.

4: I would like to be able to toggle the campaigns with posts to show and hide the results.
Not sure I follow this part, sorry! Are you saying that you would like to hide the Campaign posts with child Posts by default, but allow the front-end User to turn them on manually somehow, to override the visibility you set programmatically? Please provide some additional details here.

#1711547

Hi Christian and thank you for the response. I tried to implement the view as you stated. I was successful with the logic you provided. It was very helpful.

For the final part, #4: You are correct. I would like to hide all of the campaigns that have a child post where the author id matches the current user. This would basically hide any campaign that had a child post that the current user has already created a relationship for.

#1711813

Okay I see, you'd like to make it easy for your users to switch between a list of all the Campaigns and a list of Campaigns with no child posts. As discussed, there is no built-in custom search filter based on child post existence. You could create two Views - one that shows all the Campaigns regardless of child posts and another that uses the technique described above to show only childless parent Campaigns. Place those two Views on two separate Pages that otherwise look identical, and create links between the two Pages so that it's easy to navigate back and forth between the two. You could create the links so they look like toggle switches or filters. That's probably the most effective way to set this up without any custom code.

To set up a View that has an actual working filter, you would have to create a custom field in the parent post type that stores a value to indicate whether or not the post has children, like a radio button with two options (this will work better than a single checkbox in filters). Then you would have to use custom code with our Post Relationships API to set the value of that field whenever the Campaign is linked to a child Post or unlinked from a child Post. We have two APIs to listen for those events:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_association_created
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_before_association_delete

#1713487

Is it possible to use the same logic of putting data in the parent post, but using a repeating field group?

#1713809

If you mean you want to replace the child Posts and post relationship with a repeatable field group (RFG) applied to the parent Campaign post then yes, the logic is similar. In either case, you would have to create a custom field on the parent Campaign post type that has different values for "this Campaign has RFG" or "this Campaign does not have RFG", or something similar. Then set up a View of Campaign posts with a search filter based on that custom field. Your custom code must listen for an event when RFG posts are created or deleted, and must update the custom field value in the parent Campaign post accordingly. In either case, your code must use the post relationships API to query for child posts of the Campaign post. If you use RFG, the RFG is treated as a child post in a one-to-many post relationship with the parent Campaign post. The post type slug in this case is the same as the slug of the RFG.

#1715403

My issue is resolved now. Thank you!