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: ")
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.
My issue is resolved now. Thank you!