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?
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
Perfect! Thanks, Shane! Happy New Year!
Shane,
Is there any reason this would stop counting at 10?
I only ask because it stops counting at 10.