Skip Navigation

[Resolved] How to filter child posts in a loop for duplicates

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/Karachi (GMT+05:00)

This topic contains 5 replies, has 2 voices.

Last updated by Waqar 11 months, 3 weeks ago.

Assisted by: Waqar.

Author
Posts
#2656023

***Tell us what you are trying to do?***

Currently we have three different custom post types: clinics, therapists, and treatments.
Right now they are all separate posts types(not using intermediate posts).

"clinics" and "therapists" are self-explanatory, and "treatments" are the treatments offered by each clinic. Currently, the treatments are connected directly to each clinic, but we are trying to adjust things so that treatments are connected to each therapist, so that if a therapist is moved to another clinic, the treatments are moved automatically.

I was able to successfully add a relationship between therapists and treatments, and successfully show each treatment connected to a therapist in a view. So I am able to list all physical therapists on the clinic page, along with the treatments they offer.

The problem that I am having is displaying a combined list of all the treatments connected to therapists, that are connected to that clinic. I can get them all to show up by creating a view of all connected therapists, that in turn shows all connected treatments, but then I get duplicates because multiple therapists can be connected to the same treatments(because multiple therapists might be accredited to treat concussions, for example).

Is there a way that I can filter the total list of treatments for each clinic through that loop so as to get rid of duplicates? Or is there a completely different way that I should be doing things to display this information?

***Is there any documentation that you are following?***

It seemed like there were somewhat similar support tickets that others had submitted, where they were trying to filter within loops, but they seemed different than my use case.

I'm essentially trying to do the opposite of this support ticket(filtering child info from parent view, instead of filtering parent info, from child view :
https://toolset.com/forums/topic/remove-duplicate-parents-from-views-loop-with-wpv_filter_query_post_process/

It sounded like there was a simple solution when only a simple list is needed, which is what I'm trying to achieve, but that solution was not shared because the original poster was looking for a solution that would work with a complex grid.

I was reading some of these articles to try and come up with a solution:
https://codex.wordpress.org/The_Loop#Multiple_Loops_in_Action
https://toolset.com/documentation/legacy-features/views-plugin/using-shortcodes-in-conditions/

#2656167

Hi,

Thank you for contacting us and I'd be happy to assist.

Since the final treatments list will be generated by multiple instances/queries of the views, it is not possible to remove the duplicates at the query level.

An alternate is to wrap each treatment list item, in a container with a special class name where the post ID of the current treatment post is used.

Next, a custom script can be used to cycle through all those list item containers and keep only their first instance visible.

You'll find a similar usage example with a code snippet, in the following support forum reply:
https://toolset.com/forums/topic/split-how-to-remove-duplicated-results/#post-2029279

I hope this helps and please let me know if you need further assistance.

regards,
Waqar

#2657053

Hmm, I'm struggling a little bit to understand how I would adapt the solution you pinned to my current problem. Would I do something like this:

Outer Loop(cycling through each therapist connected the clinic):

[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
	<ul class="wpv-loop js-wpv-loop result-items">
		<wpv-loop>
			<li>
				[wpv-view name="treatments-offered-by-therapist"]
			</li>
		</wpv-loop>
	</ul>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
		<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
	[/wpv-no-items-found]
[wpv-layout-end]

jQuery in outer loop:

jQuery(document).ready(function(){
    jQuery('ul.result-items li').each(function() {
        var classNames = jQuery(this).attr("class");
        jQuery('li.'+classNames).not(':first').hide();
    });
});

Inner Loop(cycling through each treatment connected to therapist):

[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
	<ul class="wpv-loop js-wpv-loop">
		<wpv-loop>
			<li class="result-item-[wpv-post-id]">
				[wpv-post-title]
			</li>
		</wpv-loop>
	</ul>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
		<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
	[/wpv-no-items-found]
[wpv-layout-end]

This current solution does not work, but I'm hoping it is close to what you were trying to explain to me?

I imagine it is a problem with the fact that the `li` elements in the inner loop are not direct children to the `ul` in the outer loop? Perhaps I need to use [wpv-post-link] in the outer loop to reference the treatments directly, using .child or something?(I need an anchor link anyways)

How would I go about doing that?

#2657301

Yes, it seems like the custom script part will need some adjustment.

Can you please share a link to an example page where these duplicated entries can be seen?

#2657485

Thanks for getting back to me so quickly, you've been very helpful! It turns out the script IS working, but the view preview in elementor just wasn't displaying the view with the jQuery applied, but once I had actually published the page, it is filtering the results correctly.

I am having one related problem, which maybe requires a new forum thread and this one marked as resolved?

The problem I'm having is that originally it shows all results, and then hides the ones that should be hidden after a second or so. If there was any way we could fix that, that would be phenomenal, but that might be being caused by other plugins with caching/lazy loading, so maybe it is not Toolset doing this behavior? I don't know if there's a way to force the jQuery to complete before displaying the results.

You can see this happening in this test clinic I created here: hidden link

#2658441

Thanks for the update and glad that I could help.

The slight flash of those duplicate entries is expected, since the script executes on the frontend, once the page loading has completed.

I'm afraid, I don't have any idea on how to overcome this, but for even better suggestions and recommendations, you can consult specialist jQuery-related community forums.

#2659257

Awesome, thanks for all your help. I'll have to look into the jQuery load issue some more.

Doug confirmed that the issue was resolved on 2023-11-01 20:28:20.
This ticket is now closed. If you're a Toolset client and need related help, please open a new support ticket.