Skip Navigation

[Resolved] Return The Number of Child Posts in a Parent View

This support ticket is created 3 years, 10 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)

Tagged: 

This topic contains 3 replies, has 2 voices.

Last updated by Ed 3 years, 10 months ago.

Assisted by: Shane.

Author
Posts
#1894957

Ed

I want to show the number of child posts in a parent view.

Referencing this ticket: https://toolset.com/forums/topic/display-child-post-count-in-parent-posts-table/

Luo,
I have this working, but it shows ALL child posts, with ANY status (including Drafts).

I'd prefer to only show the number Published posts. Is that possible?

#1895115

Shane
Supporter

Languages: English (English )

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

Hi Ed,

Thank you for getting in touch.

The solution that Luo had provided is no longer valid for this version of Toolset as our Relationship API has changed since then.

If you are running the latest version of our Toolset plugins the method you should use is this below.
https://toolset.com/documentation/customizing-sites-using-php/displaying-child-posts/#displaying-child-post-contents

add_shortcode('review-count', 'review_count_func');
function review_count_func()
{

$child_args = array(
    'post_type' => 'reviews',
    'numberposts' => -1,
'post_status'=>'publish',
    'order' => 'ASC',
    'toolset_relationships' => array(
        'role' => 'child',
        'related_to' => get_the_ID(),
        'relationship' => 'my-relationship-slug'
    )
);
 
$query = new WP_Query( $child_args );
$child_posts = $query->posts;
return count($child_posts);
}

Please let me know if this helps. Replace "my-relationship-slug" with the relationship slug of your Parent/Child relationship.

Thanks,
Shane

#1895289

Ed

Perfect! Thanks, Shane! Happy New Year!

#1926913

Ed

Shane,
Is there any reason this would stop counting at 10?

I only ask because it stops counting at 10.