I am trying to: Use a shortcode to display total child post count (post relationship is one to many). Name of parent post is 'Campsite' and child post is 'Review'. Kindly help.
Here is the code I have added in the snippets:
function func_get_total_campsite_review( $atts ){
global $post;
$childs = toolset_get_related_posts(
$post->ID, // the parent post
array('campsite','review'), // the RFG slug
'parent', // the RFG role in this relationship is 'child'
10000, // the maximum number of results
0, // the offset
array(), // additional query arguments
'post_id', // return format
'child', // role to return
'',
'ASC'
);
return count($childs);
}
add_shortcode( 'show_total_campsite_reviews', 'func_get_total_campsite_review' );
Link to a page where the issue can be seen: NA.
I expected to see: Total count of child posts.
Instead, I got: 0
Hi, a couple of things:
1. The array syntax only works for migrated post relationships. If you created this post relationship after migration, or if you created this post relationship in the new relationships system, this line should be changed:
array('campsite','review'), // the relationship slug
Instead of an array, you should use the post relationship slug as a string. If you are not sure of the post relationship slug, you can find it in Toolset > Relationships.
2. Depending on where you are using this shortcode, you may not be able to rely on the global $post to get the parent post ID. You can test this out by returning the post ID to confirm the parent post ID is correctly set:
Let me know if you continue to experience problems and I can take a closer look.