Skip Navigation

[Resolved] How to skip duplicate items in a loop?

This support ticket is created 4 years, 9 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 3 replies, has 2 voices.

Last updated by Luo Yang 4 years, 9 months ago.

Assisted by: Luo Yang.

Author
Posts
#1492861

I am using a nested view to loop through children of a parent post in order to return taxonomy terms associated with the child posts.

Relationship (many to many) is as follows:
- Book Series << Series Number >> Audio Book
- Audio Books are associated with the taxonomy titled 'Author'

Because some of the child posts are tagged with the same taxonomy term, the loop outputs duplicates of the same term. Example:

Book Series A (parent post page)
>> Audio Book 1 (child) >> Author 1 (taxonomy term)
>> Audio Book 2 (child) >> Author 1 (taxonomy term, duplicate)
>> Audio Book 3 (child) >> Author 1 (taxonomy term, duplicate)

How can I skip or remove the duplicate terms?

#1493193

Hello,

Since the taxonomy "Author" is in post type "Audio Book", and you are querying the "Audio Book" posts, so it is expected result:
there will be duplicated "Author" terms

You might consider to register the taxonomy "Author" to post type "Book Series", then display the result with a nested view:
1) Edit each "Book Series" post, setup taxonomy "Author" terms.

2) Create the parent taxonomy view:
- Query terms of taxonomy "Author"
- Filter by a "Taxonomy filter":
Select posts with taxonomy:
Authors the same as the page where this View is shown
- In view's loop display the "Author" term's title + below child post view.

3) Child post view:
- Query "Audio Book" posts
- Filter with:
a) Post type relationship filter: Select posts in a Series Numbers relationship that are related to the Post where this View is shown.
b) Taxonomy filter:
Select posts with taxonomy:
Authors set by the parent Taxonomy View
- In Views loop, display the "Audio Book" post information

4) In single "Book Series" post, display the parent taxonomy view of step 2)

More help:
https://toolset.com/documentation/user-guides/views/using-a-child-view-in-a-taxonomy-view-layout/

#1493255

Hi Luo,

Thanks for your description. The issue here is that not every Audio Book in a series has the same authors.

If the taxonomy is associated with the Book series (parent) rather than with the audio book (child), how can I define which authors are related to which book?

I am hoping to display the aggregate total of authors associated with any audio books of a series, instead. Is there a way to do this while skipping over any duplicate posts?

#1493295

Q1) how can I define which authors are related to which book?
You can register to taxonomy "Author" to both post types:
- Book Series
- Audio Book
But you will need to setup "Author" terms in each "Book Series" and "Audio Book" post.

Q2) Is there a way to do this while skipping over any duplicate posts?
There isn't such kind of built-in feature within Toolset plugins using your website existed settings, as I mentioned above:

Since the taxonomy "Author" is in post type "Audio Book", and you are querying the "Audio Book" posts, so it is expected result:
there will be duplicated "Author" terms

you might consider the workaround I mentioned above.