Skip Navigation

[Resolved] How to output a hierarchical post type respecting its hierarchy

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

Problem:
A post type is hierarchical, meaning the posts can have child posts, which can have child posts etc.

These should be output in a list with each parent showing its child posts indented etc.

Solution:
You need nested Views, one for each level of the hierarchy.

The first View displays only the parents by including a Query Filter to only return posts without a parent post (i.e. the top level posts).

In the output section, between the wpv-loop tags, a second View can be inserted to display the child posts, which would be achieved with a Query Filter "Parent is the current post in the loop".

Relevant Documentation:
https://toolset.com/documentation/user-guides/using-a-child-view-in-a-taxonomy-view-layout/

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

Last updated by camila 5 years, 11 months ago.

Assisted by: Nigel.

Author
Posts
#1164797
child-posts.png
assign-parent-page.png

Hi, Great plugin!

I am using your plugin for a website and it's working great. I am having a challenge with one of my custom post types. I would like to show them in the front end as nested elements. Ex:
1.1 Dummy Text
1.1.1 Child Element
1.1.1.1 Child Child Element

All these items belong to the same custom post type, and I am creating the relationship using the built-in Page Attribute feature in WordPress to assign Parent Pages. (see screenshot).

At the moment I am using a View to display the custom post types items in my front end. Code Below:

[wpv-layout-start]
[wpv-items-found]
<!-- wpv-loop-start -->
<wpv-loop wrap="1" pad="true">
[wpv-item index=1]
<div class="individual-study">
<div class="col-sm-12">
[wpv-post-body view_template="Loop item in Individual Studies View"]
</div>
</div>
</wpv-loop>
<!-- wpv-loop-end -->
[/wpv-items-found]
[wpv-no-items-found]
[wpml-string context="wpv-views"]No items found[/wpml-string]
[/wpv-no-items-found]
[wpv-layout-end]

So it's basically looping through the element and listing them. But I would need some kind of conditional, to identify if the post had a parent page so we can nested inside and add a class. I am thinking something like this:

[wpv-layout-start]
[wpv-items-found]
<!-- wpv-loop-start -->
<wpv-loop wrap="1" pad="true">
[wpv-item index=1]
"if post has not parent page show"
<div class="individual-study" class="first-level">
<div class="col-sm-12">
[wpv-post-body view_template="Loop item in Individual Studies View"]
</div>
</div>
"end if" else "if post has 1 parent page show"
<div class="individual-study" class="second-level">
<div class="col-sm-12">
[wpv-post-body view_template="Loop item in Individual Studies View"]
</div>
</div>
"enf if" else "if post has 2 parent page show"
<div class="individual-study" class="third-level">
<div class="col-sm-12">
[wpv-post-body view_template="Loop item in Individual Studies View"]
</div>
</div>
</wpv-loop>
<!-- wpv-loop-end -->
[/wpv-items-found]
[wpv-no-items-found]
[wpml-string context="wpv-views"]No items found[/wpml-string]
[/wpv-no-items-found]
[wpv-layout-end]

Is that possible, also that they are nested, so the second level inside the first level and third level inside the second level?

Thanks,

Vicky

#1164900

Nigel
Supporter

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

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

Screenshot 2018-12-12 at 15.43.07.png

Hi Vicky

What you are describing wouldn't really work because you wouldn't necessarily get the posts all output in just the right order.

You need nested Views.

The first View will query the post type and include a Query Filter to only return posts without a parent post (i.e. the top level posts), as shown in the screenshot.

The Loop Output section might look a little like this:

<wpv-loop>
<p>[wpv-post-title]</p>
[wpv-view name="Second level"]

See that I'm including a nest View. This View would essentially be the same, but the Query Filter this time would be the "Parent is the current post in the loop", and the loop output might indent the title and would include a third View.

You could go on like this for as many levels as you have in the hierarchy.

You will likely want to delete everything within the wpv-no-items-found shortcodes to avoid outputting anything where there are no descendent posts.

#1164942
2018-12-12_1155.png

hi thanks, make sense.

I have my first view with the following: - post parent filter (no parent)

[wpv-layout-start]
[wpv-items-found]
<!-- wpv-loop-start -->
<wpv-loop wrap="1" pad="true">
[wpv-item index=1]
<div class="individual-study">
<div class="col-sm-12">
[wpv-post-body view_template="Loop item in Individual Studies View"]
</div>
<div class="second-level">
[wpv-view name="Second Level Individual Studies"]
</div>
</div>
</wpv-loop>
<!-- wpv-loop-end -->
[/wpv-items-found]
[wpv-no-items-found]
[wpml-string context="wpv-views"]No items found[/wpml-string]
[/wpv-no-items-found]
[wpv-layout-end]

my second view:

I have my first view with the following: - post parent filter (Parent is the current post in the loop)

[wpv-layout-start]
[wpv-items-found]
<!-- wpv-loop-start -->
<wpv-loop wrap="1" pad="true">
[wpv-item index=1]
<div class="individual-study">
<div class="col-sm-12">
[wpv-post-body view_template="Loop item in Individual Studies View"]
</div>
<div class="second-level">
[wpv-view name="Second Level Individual Studies"]
</div>
</div>
</wpv-loop>
<!-- wpv-loop-end -->
[/wpv-items-found]
[wpv-layout-end]

But it's not working for me. the first part it's working, so now I am getting only the top pages listed, but the second view it's not.

Am I missing something?

Thanks so much!

#1164957

Hi I think i got it working now for the second and third view i have to remove another query filter i had.

Thanks!

#1164977

My issue is resolved now. Thank you!