Skip Navigation

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

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.

This topic contains 1 réponse, has 2 voix.

Last updated by Christian Cox Il y a 1 an et 5 mois.

Assigned support staff: 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

Christian Cox
Supporter

Languages: Anglais (English )

Timezone: America/New_York (GMT-04:00)

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.

Le sujet ‘[Fermé] Shortcode to display total child post count in one to many relationship’ est fermé à de nouvelles réponses.