Skip Navigation

[Resolved] Trying to count childeren of a parent

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to create a custom shortcode that displays the number of child posts for the current parent post. I'm referencing an older ticket but it does not seem to work as expected.

Solution: Instead of the _wpcf_belongs_slug_id postmeta key, you must use the new post relationships API with post relationships created in Types 3.0+. A custom shortcode solution is available.

add_shortcode( 'tssupp-connections', function( $atts = [] ){
   
    // provide defaults
    $atts = shortcode_atts( 
        array(
            'relationship'      =>   '',
        ), 
        $atts
    );
   
    global $post;
    $count = 0;
   
    $relationship = toolset_get_relationship( $atts['relationship'] );
   
    if ( $relationship ) {
   
        $parent = $relationship['roles']['parent']['types'][0];
        $child = $relationship['roles']['child']['types'][0];
        $type = $post->post_type;
   
        $origin = ( $parent == $type ) ? 'parent' : 'child';
   
        // Get connected posts
        $connections = toolset_get_related_posts( $post->ID, $atts['relationship'], array(
            'query_by_role' => $origin,
            'role_to_return' => 'other',
            'need_found_rows' => true )
        );
        $count = $connections['found_rows'];
   
    }
   
    return $count;
});

Use it like this:

Number of children: [tssupp-connections relationship="project-task"][/tssupp-connections]

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

This support ticket is created 3 years, 1 month 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by wesselK 3 years, 1 month ago.

Assisted by: Christian Cox.

Author
Posts
#1970395

Tell us what you are trying to do?
I have two post_types, establishment (vestiging) and task (taak) they have a one-to-many relationship. I am now trying to display the amount of tasks conntected to a single establishment using a custom short code.

I have added the follwing code to my function.php:
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_vestiging_id', 'value' => $parent_post_id))); // parent cpt slug
$child_posts = get_posts( $child_posts );
return count( $child_posts );
}

And then added this shortcode to the page:
[wpv_child_post_count parent_post_id='[wpv-post-id]' child_post_type='taak']

But for some reason it keeps returning 0 even though there are multiple tasks connected too a establishment.

Is there any documentation that you are following?
https://toolset.com/forums/topic/count-child-post/

Is there a similar example that we can see?
https://toolset.com/forums/topic/count-child-post/

What is the link to your site?
hidden link (the shortcode is inserted behind "total: ")

#1971255

I am now trying to display the amount of tasks conntected to a single establishment using a custom short code....But for some reason it keeps returning 0 even though there are multiple tasks connected too a establishment.
Hello, since Types 3.0, Toolset's post relationships no longer use the '_wpcf_belongs_slug_id' postmeta value to establish post relationships. The ticket you're referencing includes instructions relevant to the legacy post relationships system, but is no longer applicable if your site uses the new post relationships system. To determine if your site is using the new post relationships system, go to wp-admin > Toolset > Relationships. You will either see a dashboard list of all your site's post relationships, or a message prompting you to migrate to the new post relationships system. If you see the dashboard, your site is already using the new post relationships system and you can no longer rely on the _wpcf_belongs_slug_id meta key method to query related posts. You should use the new Post Relationships API toolset_get_related_posts instead:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

I suspect that your site uses the new post relationships system, and you will see a list of relationships in Toolset > Relationships. If that is the case, I have a custom shortcode ready to use in the parent post or parent post template to display the number of related child posts from this (or any other) one-to-many (O2M) post relationship. Here's the PHP code:

/**
 * tssupp-connections - a custom shortcode that returns the number of child posts for the current parent post
 *
 * @att (string) relationship : O2M post relationship slug
 * @return (string): number of child posts
 * 
 * Example: This Establishment has [tssupp-connections relationship="est-tsk"][/tssupp-connections] Tasks
 * Reference: https://toolset.com/forums/topic/trying-to-count-childeren-of-a-parent/
 */
add_shortcode( 'tssupp-connections', function( $atts = [] ){
  
    // provide defaults
    $atts = shortcode_atts( 
        array(
            'relationship'      =>   '',
        ), 
        $atts
    );
  
    global $post;
    $count = 0;
  
    $relationship = toolset_get_relationship( $atts['relationship'] );
  
    if ( $relationship ) {
  
        $parent = $relationship['roles']['parent']['types'][0];
        $child = $relationship['roles']['child']['types'][0];
        $type = $post->post_type;
  
        $origin = ( $parent == $type ) ? 'parent' : 'child';
  
        // Get connected posts
        $connections = toolset_get_related_posts( $post->ID, $atts['relationship'], array(
        	'query_by_role' => $origin,
        	'role_to_return' => 'other',
        	'need_found_rows' => true )
    	);
    	$count = $connections['found_rows'];
  
    }
  
    return $count;
});

Use it like this in the parent post or parent post template:

Number of children: [tssupp-connections relationship="project-task"][/tssupp-connections]

Replace project-task with the slug of this O2M post relationship, which you can find in wp-admin > Toolset > Relationships.

#1971877

My issue is resolved now. Thank you!

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