Skip Navigation

[Résolu] Shortcode to display total child post count in one to many relationship

This support ticket is created Il y a 4 années et 2 mois. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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 1 réponse, has 2 voix.

Last updated by Christian Cox Il y a 4 années et 2 mois.

Assisted by: Christian Cox.

Auteur
Publications
#1510633

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

#1511689

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:

return $post->ID;

Let me know if you continue to experience problems and I can take a closer look.

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