Skip Navigation

[Resolved] Count Child Post (2)

This support ticket is created 4 years, 8 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)


This topic contains 21 replies, has 3 voices.

Last updated by pavelK 4 years, 8 months ago.

Assigned support staff: Minesh.


I'm trying to repeat it -

Help with this solution - for my case, please.
I repeated this code, changed the values, but the shortcode shows 0.
I use the beta version of the toolset. There are 2 post type: Providers (post_type=service-provider) and Programs (post_type=learning-program). They are in the relationship - Providers (1) parent, and the Program (3-5, a few from each provider) - child.
I can't figure out how to write a slug (parent cpt slug) for my provider (parent) ...and might be wrong somewhere?
what will this code look like with my values? (parent post type - 'service-provider' и child post type 'learning-program')...


I assume you migrated the Custom Post Types relationships to the new Many to Many structures since you use the beta Types version, correct?

Hence, the old API will not be correct to use anymore, and that code snippet is based on the old API.
There is a new API, which you can use to craft your custom code snippets:

We do not craft custom code as you request it here.
We cannot make it "so it's adapted to your case".
But, we can give the correct example and information about the API, and with your knowledge of PHP and WordPress API, you can then craft custom code as per needs.

Let me show you another code that does exactly the same.
The example that Noman gives is wrong, it uses a wpv_ prefix, something that is reserved to Toolset ShortCodes and should never be used elsewhere as a prefix.
What if we ever were to add such a ShortCode to our code base? Accidentally, given that we prefix all our ShortCodes with a "wpv_" prefix in the PHP, it might then conflict!
It's hence better to craft the own, particularly prefixed codes for your specific site/theme/plugin.

This was my example when you use the Old API.

 *Count child posts of given type
function child_post_count() {
    $post_id = $post->ID; //get current post ID
    $child_post_count = count( get_posts( array( 
    'post_type' => 'your_child_post_type_slug', //child post type
    'meta_key'         =>  '_wpcf_belongs_your-parent-post-type-slug_id',
    'meta_value'       => $post_id,  
) ) );
    return $child_post_count;//this returns a numerical value of Posts that are of "child" type, and child of the $post_id

This worked with the old Relationships

With the Types beta, or even Types 2.2.22 you can already use the new API:

To help you craft this code sample, I need to know the exact nature of your Post Relationship.
Has this relationship been set up with the old Types and now has been migrated to new Types, or, is this a complete newly set up relationship?

The function you will use will look something like this:

function child_post_count() {

    //get the posts
    $books = toolset_get_related_posts( 
    $writer, // in your case this is learning-program
    array( 'writer', 'book' ), // service-provider and learning-program
    'child', // get posts where $writer is the child in given relationship
    $posts_per_page, $current_page, // pagination
    array( ), // nothing to filter additionally by
    //Count those posts
    $amount = count($books->ID);

    //return the value
    return $amount;

Beda, thanks for the reply! This is a complete newly set up relationship. Toolset Types beta version 2.3-b2. While I can not get the result, unfortunately. How to use this code for shortcode, - what shortcode to use for display?



Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Beda is not available currently. This is Minesh here and I'll try to help you further. I hope this is OK.

Well - first of all, I would like to know - which way you want to go - do you want to go with Toolset Stable plugins - it's recommended. If yes - with stable plugin.

You can use following code - add following code to your current 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_service-provider_id', 'value' => $parent_post_id))); // parent cpt slug
    $child_posts = get_posts( $child_posts );
    return count( $child_posts );

And now, call above shortcode as:

[wpv_child_post_count parent_post_id='[wpv-post-id]' child_post_type='learning-program']

Where - as you can see child_post_type='learning-program' and parent is service-provider.


Thanks, Minesh! The problem is that the beta version of the plugin is installed. I used this code, but ... 0 is displayed. I inserted the shortcode on the page (post) provider.



Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

The code I shared is for stable version of Toolset plugins.

For betas - the code will be different. But I would like to know here - did you migrate the old post relationship to new M2M?

*** Please make a FULL BACKUP of your database and website.***
Could you please go to:
=> Toolset => Relationship => If you see message to migrate the post relationship date - please do that


I have installed the beta version from the beginning (with 0) - Toolset Types 2.3-b2 and Toolset Views 2.6-b1. The old post relationship did not migrate to new M2M. Absolutely all relationships were created on the beta version with 0. There are no messages in this section => Toolset => Relationship



Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

First of all - Could you please update your Beta plugins to latest beta version Types 2.3-b5 and Views 2.6-b3.

Now - the code I shared was to use with old post relationship. With new post relationship I will try to share the new shortcode but I would like to know the slug of your relationship you made between your post type.

As well as - is it possible for you to send me access details so I can add working code to your current setup. As well as problem URL where you want to display the child post count.



Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Thanks for sharing access details.

Well - the new Relationship API is not available with Toolset Types 2.3-b2 and Toolset Views 2.6-b1 - so I've updated those plugins to latest beta version.

Now - we debugged a lot but somehow on your site we do not get the desired result but with our local test site - we do get the correct post count.

Also, I would like to add here - is this production site? If yes: I wonder why you are using Beta plugins as it not recommended for production.

I would like to have duplicator copy of your site so we can test is with minimum set of plugin and default theme so we can check whats going wrong there.

More info:

I have set the next reply to private which means only you and I have access to it.


It used to be a test site. Here I tested themes and plugins. Now it is the main production site. Unfortunately, it happened. Therefore, there is no copy of the duplicate site. I've noticed a conflict with the theme OceanWP. But the developers OceanWP are unable to help me. It is possible that the problem is related to the Ocean. But I have a license of Avada theme (+child theme), GeneratePress (+child theme). I was planning to go to the theme of GeneratePress. You can safely experiment with it. The appearance of the site and the content I adapt later. It is possible that You will give advice which theme to use in my case. But again, I was planning to use GeneratePress. I've tested the theme GeneratePress and I liked it. I'm sorry that you are wasting me a lot of time, but maybe it will be useful for the Toolset project. Please let us know if you plan to continue.



Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - we certainly not recommend this approach and please note that later on if there is something broken or loss of data Toolset will not be responsible for that.

I strongly suggest you to make a test site and test and play there and after that configure it with your live site. I see after updating the Toolset plugins to latest Beta - when you edit any post - post relationship box is not loading and I see javascript error on brower's console.

You need to check with minimum setup to check whats causing that conflict. In order to minimize the cause of the issue:
Could you please try to resolve your issue by deactivating all third-party plugins as well as the default theme to check for any possible conflicts with any of the plugins or themes?


Ok! I'll do the experiments, check everything and let you know here. Thank you for your help and tips for me.


I deactivated all plugins and installed the theme Twentyfifteen
After deactivating the plugins - no changes have occurred
After deactivating the Ocean theme, the javascript error disappeared



Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - I must say you should not build site like this - with Beta plugins you should only use Toolset Betas as number of features added to Beta plugins may not be compatible with stable plugins and results in unexpected output.

I've added following code to get count of child post but its still throwing unexpected results - it also looks like the database is corrupted as its throwing unexpected output:

$posts = toolset_get_related_posts(3696,'prov-prog','parent',999,1,array(),'post_object');
return count($posts);  

I must say you should setup a test site with only BETA plugins added and build your relationship from scratch and first of all add few dummy posts and to check relationship is working properly and if its does not you are most welcome to contact us.


Ok, I'll try to do that and will contact you. Minesh, thank you for your help.