Creating an image grid using a View
Started by: Pete
in: Toolset Professional Support
Quick solution available
2
3
4 years, 7 months ago
Pete
Trying to count childeren of a parent
Started by: wesselK
in: Toolset Professional Support
Quick solution available
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
2
3
4 years, 7 months ago
wesselK