Skip Navigation

[Resolved] Counting children of each parent

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

Problem: I have a View of parent posts. I would like to show the number of child posts for each parent post inside this View.

Solution:
1. Create a View to children posts. Choose the child post type in Content Selection and add a filter by post relationship. Choose “Select posts that are a children of the Post set by parent View.”
2. Then use the [wpv-items-count] shortcode inside of the output section to count items and display the result.
3. Now you must insert the children View inside of the parent view Loop Output editor. Make sure you place the View outside of the tags, or it will be repeated.

Relevant Documentation:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-found-count

This support ticket is created 7 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.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 4 replies, has 2 voices.

Last updated by J S 7 years, 4 months ago.

Assisted by: Christian Cox.

Author
Posts
#523192

J S

I am trying to: Count child posts and display the number in a table showing on parent page.

I visited this URL: https://toolset.com/forums/topic/how-to-display-how-many-children-does-a-post-have/

How do I populate the Number of Policies field in this table with the count of policies (children) for each institution (parent)? hidden link

Also, I would like to know how to create an alternative version of this table that can be toggled on/off or shown on another page to display all institutions or only those with at least 1 policy. This way the table of 5,000+ institutions can be viewed but as most of them will not have policies initially, a shortened display of the table could be viewed showing only those with policies.

Thanks!

#523206

Hi, I'll be glad to help you set this up.

How do I populate the Number of Policies field in this table with the count of policies (children) for each institution (parent)?
1. Create a View to children posts. Choose the child post type in Content Selection and add a filter by post relationship. Choose “Select posts that are a children of the Post set by parent View.”
3. Then use the [wpv-items-count] shortcode inside of the output section to count items and display the result. More information about that here: https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-found-count
4. Now you must insert the children View inside of the parent view loop.

The day is ending for me here, so unfortunately I didn't get a chance to answer your second question, which is a bit more complex and may require some custom code. I will be back online Sunday to follow up, or if you want a more timely answer feel free to open another ticket and leave a comment here to that effect.

#523225

J S
Screen Shot 2017-05-11 at 5.24.50 PM.png

Ok thanks for the reply.

I added the [wpv-items-count] tag as you instructed in the child view below. The issue now though is the count value that is reported is showing up the number of times that the value is display. In other words a parent with 3 children is showing up as 3 3 3 and a parent with 9 children is showing up as 9 9 9 9 9 9 9 9 9 etc.

 [wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
		<wpv-loop>[wpv-items-count]
		</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
		<strong>[wpml-string context="wpv-views"]No policies found[/wpml-string]</strong>
	[/wpv-no-items-found]
[wpv-layout-end]

How do I fix this. It seems like the value reported is caught in the loop as well? See last two records on this screenshot.

Here is how I inserted the code into the Parent Loop:

<td>[wpv-post-link]</td>
<td>[types field='institution-city'][/types]</td>
<td>[types field='institution-state'][/types]</td>
<td>[wpv-view name="policy_count_per_inst"]</td>

And the loop output:

[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
	<table width="100%">
		<thead>
			<tr>
				<th>[wpv-heading name="post-link"]Institution Name[/wpv-heading]</th>
				<th>[wpv-heading name="types-field-institution-city"]Institution City[/wpv-heading]</th>
				<th>[wpv-heading name="types-field-institution-state"]Institution State[/wpv-heading]</th>
                <th>[wpv-heading name="policy_count_per_inst"] Number of Policies[/wpv-heading]</th>
			</tr>
		</thead>
		<tbody class="wpv-loop js-wpv-loop">
		<wpv-loop>
			<tr>
				[wpv-post-body view_template="Loop item in All Institutions View"]
			</tr>
		</wpv-loop>
		</tbody>
	</table>
	<!-- 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]
#523948

Hi, you should move the items found tag to be outside loop tags:

[wpv-layout-start]
    [wpv-items-found]
    [wpv-items-count]
    <!-- wpv-loop-start -->
        <wpv-loop>
        </wpv-loop>
    <!-- wpv-loop-end -->
    [/wpv-items-found]
    [wpv-no-items-found]
        <strong>[wpml-string context="wpv-views"]No policies found[/wpml-string]</strong>
    [/wpv-no-items-found]
[wpv-layout-end]

Now your item count should only appear once. Also note that [wpv-found-count] and [wpv-items-count] will provide different information, depending on your View settings.

#524049

J S

Thanks this is perfect!

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