Skip Navigation

[Resolved] Summing and counting related custom post type

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.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 3 replies, has 2 voices.

Last updated by Minesh 1 year, 1 month ago.

Assisted by: Minesh.

Author
Posts
#2690217

In my application, I have 2 customized types:
- Workout: indicates when, how long and what type of exercise is performed (related post : see below)
- Exercise: describes the movement to be performed
I'd like a view that shows me, for each exercise, the title of the exercise, the number of workouts performed and their total duration.

How can I perform that

#2690267

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

I would like to know what post-relationship you created if any using post types Workout and Exercise and what are the custom fields associated with them.

Also - where you want to display the view that shows:
- for each exercise, the title of the exercise, the number of workouts performed and their total duration.

In addition to that - Could you please send me debug information that will help us to investigate your issue.
=> https://toolset.com/faq/provide-debug-information-faster-support/

#2690283

Hi,

In fact, I'm planning another sports coaching website. I have the same needs for this future site as for this one. If I find a solution for the orcs.salentin.ch site (see debug information above), I'll have no problem transposing it to my other project.

So for the orcs.salentin.ch site, I have 2 custom types : jeu and played_game
- jeu contains information about a board game
- played_game has the folowing custom field :
- jeuclub : a post reference to "jeu", only a post reference, I do not have create a relation
- game_duration : the duration of the game in minutes
- game_players : the number of players

What I want is a listing like a table with on each row :
- the title of one jeu
- the number (count) of played_game of this specific "jeu"
- the sum of all game_duration for this specific "jeu"
- the average number of players for this specific "jeu"

I also want to be able to order the listing by any of those columns.
Regards.

#2690309

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

It can be done using nested view but in your case I think using shortcode will make more sense.

And we will use the Legacy view as Legacy view will offer you more control over table design rather using blocks. To enable the legacy view, please follow the following Doc:
- https://toolset.com/course-lesson/enabling-legacy-version-of-toolset-views/

Here is the sandbox site where I tried to setup a demo and you can auto-login to it using the following link:
- hidden link

I've setup post types:
- hidden link

And custom fields as you mentioned in your previous post:
- hidden link

As you can see I've setup few entries for both post types with required field:
- hidden link
- hidden link

I've created the following view:
- hidden link
Where:
- I've called the shortcode added to "Custom Code" section as given under:

<td align="center">[show_related_post_info relationship='related-club' type='count']</td>  
-- To display related post count

<td>[show_related_post_info relationship='related-club' type='field-sum' field='game-duration']</td>
-- To display sum duration 

<td>[show_related_post_info relationship='related-club' type='field-average' field='no-of-players']</td>
-- To display average player

Where:
- replace relationship shortcode attribute value with your original post reference field slug. In this case for this sandbox site its "related-club".

Added the following Code to "Custom Code" section offered by Toolset:
=> hidden link

add_shortcode('show_related_post_info', 'func_show_related_post_info');
function func_show_related_post_info($atts) {
  global $post;
  $field_sum = $field_avg = 0;
  $relationship = $atts['relationship'];
  $type = $atts['type'];
  $field = 'wpcf-'.$atts['field'];
  
   $child_posts = toolset_get_related_posts($post->ID,
                                                    $relationship,
                                                    'parent',999,
                                                    0,array(),
                                                    'post_id',
                                                    'child');
  $total_count = count($child_posts); 
  
 
  
 if(isset($relationship) and $type=='count') {
                 return $total_count;  
 }else if(!empty($child_posts)) {
 
   	foreach($child_posts as $k=>$v):
   		$field_sum = $field_sum + get_post_meta($v,$field,true);
    endforeach;
   
      if(isset($relationship) and $type=='field-sum') {
           return $field_sum;
      }else if(isset($relationship) and $type=='field-average') {
              return $field_sum/$total_count;
      }
 }
  
return 0;
}

On the following page - I've added the view as given under:
- hidden link

[wpv-view name="show-all-jeu-post-in-table" cached="off"]

If you can see on the the frontend for the above page here is the view result:
- hidden link

More info:
- https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
- https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/#adding-custom-php-code-using-toolset
- https://toolset.com/documentation/programmer-reference/adding-custom-code/how-to-create-a-custom-shortcode/