Hello Anthony.
Thank you for contacting the Toolset Support.
In order to achieve this, you need to follow the steps below
1) Add a number custom post field to your parent custom post type, for example, with slug "count".
2) Now in your functions.php file add the code presented below, that updates that parent custom post field whenever a child post type is saved. All it does is to evaluate the available children posts for the parent selected in the current child post. Also, updates that count when a post is deleted. Remember to replace the custom post type slugs. Here parent is slug 'clinic' and child slug is 'department'.
add_action( 'before_delete_post', 'count_total_children_delete' );
function count_total_children_delete($post_id)
{
if ( get_post_type( $post_id ) == 'department' ) {
$parent_id = get_post_meta( $post_id, '_wpcf_belongs_clinic_id', true );
if($parent_id != '')
{
$childargs = array(
'post_type' => 'department',
'numberposts' => -1,
'meta_key' => '_wpcf_belongs_clinic_id',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(array('key' => '_wpcf_belongs_clinic_id', 'value' => $parent_id))
);
$totalChildCount = count(get_posts($childargs));
update_post_meta( $parent_id, 'wpcf-count', $totalChildCount);
}
}
}
add_action( 'save_post', 'count_total_children' );
function count_total_children($post_id){
if ( get_post_type( $post_id ) == 'department' ) {
$parent_id = get_post_meta( $post_id, '_wpcf_belongs_clinic_id', true );
if($parent_id != '')
{
$childargs = array(
'post_type' => 'department',
'numberposts' => -1,
'meta_key' => '_wpcf_belongs_clinic_id',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(array('key' => '_wpcf_belongs_clinic_id', 'value' => $parent_id))
);
$totalChildCount = count(get_posts($childargs));
$oldChildCount=get_post_meta( $parent_id, 'wpcf-count', true );
if( $totalChildCount != $oldChildCount ){
update_post_meta( $parent_id, 'wpcf-count', $totalChildCount );
}
}
}
}
3) Now whenever you create a new parent post, just keep in mind to add a 0 to that custom post field in order to reset the counter.
4) Now create a view listing your parent custom post type posts, order it by the "count" custom post field (descending) and use the simple loop below
<wpv-loop>
<li>
[wpv-post-title] ([wpv-conditional if="( $(wpcf-number) ne '' ) OR ( $(wpcf-number) ne '0' )"][types field='number' format='FIELD_VALUE'][/types][/wpv-conditional][wpv-conditional if="( $(wpcf-number) eq '' ) OR ( $(wpcf-number) eq '0' )"]0[/wpv-conditional])
</li>
</wpv-loop>
5) Go and create/update your children posts in order for the counters to be updated in the parent posts.
Also, keep in mind that you also have to deal with the case when a user changes parent for one of the child posts. You have to decrease the count for one parent and increase the count for the new one. It won't be that hard to do it yourself.
If you have any further issues, please let me know.
Regards