Skip Navigation

[Resolved] Displaying number of CPT that have a given parent

This support ticket is created 6 years, 5 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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+01:00)

This topic contains 3 replies, has 2 voices.

Last updated by Nigel 6 years, 5 months ago.

Assisted by: Nigel.

Author
Posts
#588345
screenshot.png
screenshot 2.png

Tell us what you are trying to do?
I am trying to display the number of CPT that have a given parent. Specifically, I am trying to display the number of "experiments" that are children of "canvas". Both are CPTs created with Types.

Is there any documentation that you are following?
I have created a view as explained in https://toolset.com/forums/topic/show-the-total-of-entries-for-each-custom-type/ and it works fine to display the total number of posts ("experiments").

What is not working as expected?
I have added a filter to the view (in fact, I experimented with two filters available in views: "Post parent filter" and "Post relationship filter") and got a shortcode in the form : [wpv-view name="view-name" wpvchildof="123"]. I pasted it in a layout and it works (partially). For example, [wpv-view name="experiment-count" ] correctly displays the total number of "experiments" (my CPT).
However, the attribute wpvchildof= does not seem to work: if I delete it, I get the total number of CPT correctly, but if I add it, I get either zero or the total number again (depending on whether I used "Post parent filter" or "Post relationship filter"). But [wpv-view name="experiment count" wpvchildof="771"] does NOT display the number of experiments that are children of post 771 (a "canvas", parent of 2 "experiments").

How can you help?
1. First, could you explain the difference between "Post parent filter" and "Post relationship filter", and tell me which one to use in this case?
2. Second, could you explain why I get the message "This will filter out posts of the following types, because they are not hierarchical: Experiments" when I use the "Post parent filter" but not the "Post relationship filter"? What does "being hierarchical" mean in this case? ("experiments" CPT are set as children of "canvas" CPT, and "canvas" is shown as parent of "experiments", isn't that hierarchical?)
3. Third, could you help me obtain the correct number of CPT that belong to a given parent only (for example, how do I display the number of "experiments" that are children of Business Blueprint (post 771, slug business-blueprint)?
4. Finally, I will need to select the parent dynamically. I mean that on a page displaying a list of "canvases" (among which you will find business-blueprint for example) in a view, I want to display the number of "experiments" that belong to each parent. So, the attribute wpvchildof= will have to be set to each parent in the loop. (Well, this is getting complicated; if it is not clear, let's deal with that after we have solved the previous items).

What is the link to your site?
You can see what I want to display here: hidden link (the number of "experiments" in each "card" comes from the view I have created as mentioned above; now, each card should only state the number of experiments from their parent, which is indicated by the title of the card).

Thanks in advance!

#588510

Nigel
Supporter

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

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

Hi Alain

Let me describe the solution to the last point which should cover what you need to know about the others.

First, just let me clear up the difference between post parent filter and post relationship filter.

WordPress posts can be hierarchical, like pages. Parent and child posts are of the same post type.

Toolset expands this concept to allow you to create post relationships between posts of different types.

The first is what the post parent filter refers to, the second the post relationships filter.

Now, to display a list of parent posts (canvas) and for each of these show how many child posts (experiments) it has, you need a nested loop.

First, make the inner View for the child posts.

So your View will return experiment posts.

You add a post relationship filter that says Select posts that are children of the current post in the loop. (When we insert this View into the outer View the current post in the loop will be each iteration of the parent canvas post.)

Now in the Loop Output section you need to use the wpv-found-count shortcode (https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-found-count), something like this:

[wpv-layout-start]
	[wpv-items-found]
<p>Number of child posts: [wpv-found-count]</p>
<!-- wpv-loop-start -->
		<wpv-loop>
		</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
<p>No child posts</p>
	[/wpv-no-items-found]
[wpv-layout-end]

That completes the inner View.

Now create the outer View which lists the parent canvas posts.

In the Loop Output section insert the child View we just created (using the Fields and Views button) wherever you want the count to show.

That's pretty much it.

If you have any problems implementing it, let me know.

#588710

Thank you Nigel,

Your explanation is very clear and your solution right to the point (point 4!).

Your explanation will help me better understand Toolset language throughout, by distinguishing the 2 types of "hierarchies".

Your solution got me going immediately to my end goal. I did not even have to modify my 2 views (inner and outer) as the only problem lied with the filter. One checkbox to change and it was done! Thanks.

May I ask why the shortcode was not working, though? The inner view is just like your code above. I thought that by creating this view with the filter set to the option "Post with ID set by the shortcode attribute wpvprchildof", I could display the number of children posts from a given parent on any page by using the proposed code: [wpv-view name="view-name" wpvprchildof="123"] where 123 is the ID of the post. Isn't that so?

Thanks again!

#589047

Nigel
Supporter

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

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

Hi Alain

Are you trying to use the same View of Experiments in different ways?

The way I described works when you are using the View of Experiments as an inner loop nested inside a Canvas View (or shown anywhere where a Canvas post is the current post in the Loop, e.g. on a template for Canvas posts).

Where you describe using the shortcode attribute to set the Canvas parent, you would use that in a context where the Canvas parent wasn't the current post. And then it should work correctly, that you set the id as an attribute where you insert the View shortcode.

Each method should work when used in the right context (based upon what the current post is).

If you want to achieve both, you'll need separate Views for Experiments, each with the required filter.

Does that make sense or can I help further?

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.