Skip Navigation

[Resolved] Count Child Post

This support ticket is created 6 years, 11 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
- 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 -
- 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

Tagged: 

This topic contains 1 reply, has 2 voices.

Last updated by Noman 6 years, 11 months ago.

Assisted by: Noman.

Author
Posts
#513945

I had built 2 custom post type with Toolset - Speaker Profile and Speaker Post.
Speaker Profile is the cpt for user to build their profile and display to others and speaker post is the post publish by speaker on their profile.
I will like to display the number of post published by each speaker on their profile page.

How can I get the count of number of post?

thanks for helping 🙂

#514013

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Win Soh,

Thank you for getting in touch with us. Please add below code in your theme’s or child theme’s functions.php file:

add_shortcode('wpv_child_post_count', 'wpv_child_post_count_fun');
function wpv_child_post_count_fun($atts) {
	
    extract( shortcode_atts( array(
		'parent_post_id' => '',
		'child_post_type' => '', // child cpt slug
    ), $atts ) );
  
	$child_posts = array( 'post_type' => $child_post_type, 'meta_query' => array(array('key' => '_wpcf_belongs_student_id', 'value' => $parent_post_id))); // parent cpt slug
	$child_posts = get_posts( $child_posts );
	return count( $child_posts );
}

==> Replace student with your Parent CPT slug.

==> After that please use this Shortcode in you View or View’s loop to display child posts count, and make sure to replace child_post_type with your child post type slug, here I have used ’speaker’ in the below shortcode:

[wpv_child_post_count parent_post_id='[wpv-post-id]' child_post_type='speaker']

We have another client who had the similar issue and was also able to solve it: https://toolset.com/forums/topic/counting-children-of-post/

I hope it helps, Thank you

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