Skip Navigation

[Resolved] loop through categories used by child posts of current parent post

This thread is resolved. Here is a description of the problem and solution.

Problem:
The issue here that the user wanted to display his Parent/Child post in the format
Category one
- article
- article
Category three
- article
- article
- article
Category six
- article
Category ten
- article
- article
- article
- article

Solution:
This can be done using a nested view setup
I would recommend taking a look at the link below as it goes through the steps on how to setup this.
https://toolset.com/documentation/user-guides/using-a-child-view-in-a-taxonomy-view-layout/

This support ticket is created 6 years, 11 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 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 5 replies, has 2 voices.

Last updated by Jim 6 years, 11 months ago.

Assisted by: Shane.

Author
Posts
#598503

Jim

I created a custom post type (journal) with a child post type (article)

What I want to do is:
Create a view that lists the name of all category items in use by the articles of the parent post, with the articles that have that category checked listed beneath the category name.

It is easy with this tutorial and that works perfect: https://toolset.com/2015/07/how-to-group-posts-by-category/
That's how I want it.

But....
It also shows categories that have no articles (because that journal has no articles with that category).

So how can I get the above result but hiding the empty categories. Like this:

Journal page
Category one
- article
- article
Category three
- article
- article
- article
Category six
- article
Category ten
- article
- article
- article
- article

Is this possible?

#598590

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jim,

Thank you for contacting our support forum.

I would recommend taking a look at the link below as it goes through the steps on how to setup this.
https://toolset.com/documentation/user-guides/using-a-child-view-in-a-taxonomy-view-layout/

Please let me know if this helps.
Thanks,
Shane

#598629

Jim

Hi Shane,

That's not really the same situation here.
Like the example I linked to.

In my case it is about three levels deep:

Parent post
- select child posts from that parent
- select the categories used by those child posts
- display the first category that has at least one linked child post
- display a loop of all the child posts with that category
- display the next category that has at least one linked child post
- display a loop of all the child posts with that category
etc...

Please read my first question to understand the exact situation.
Thanks!

#598687

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jim,

Since its 3 levels deep then you will need to Create 3 views.

The documentation should've provided you with some background information on the filters to use.

So first you will have a Parent view that lists the Journals.

Journals View
- Child View (lists children of journals)
- Categories view that lists Categories of that children.
- Another Child view of Journals with another filter for the Categories View

This is how I imagine the structure would be, In this case we would need a nesting of 4 different views.

Please let me know if this sounds more like what you are trying to achieve.
Thanks,
Shane

#599202

Jim

Hi Shane,
That is probably a solution.
I am not sure if we need the first Journal view because this is placed on a single journal page:

1. Journal
2. - Child View (lists children of the current parent)
3. - Categories view that lists Categories of that children.
4. - Another Child view of Journals with another filter for the Categories View

Does this look good to you?
I am not sure how but if you know...

#599267

Jim

Found a solution!!!

In the parent view I loop through all the categories.

So I reach the first category and now instead of printing the category name before I know if there are articles with this category I pass the category name as an argument to the child view!

In this child view after "[wpv-items-found]" right before the loop starts I print the name of the category!

Here we know there are items to be shown so the category is not empty and can be printed.

Below there is the "[wpv-no-items-found]" and there I just deleted the "No items found" message.

Tadaaaaa

Me so happy