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 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 4 years, 8 months ago.
Assisted by: Christian Cox.