Problem:
I have to CPTs: Voyage and Ticket
Ticket CPT is a Child of Voyage CPT
Ticket CPT has a field titled total-tickets-available
A Voyage has 4 Child Tickets:
Ticket A total-tickets-available value is 5
Ticket B total-tickets-available value is 10
Ticket C total-tickets-available value is 2
Ticket D total-tickets-available value is 2
What I need to do is summarise these values and display the total (which would be 19 in this example) in the Parent Voyage.
Solution:
There ins't such kind of feature within Views plugin, Views is for displaying data, it can not calculate data, so in your case it needs custom codes, here is a example with detail steps:
1) create a custom shortcode to display the sum value, modify your PHP codes as below:
add_shortcode('sum-of-tickets-available', 'sum_of_tickets_available_func'); function sum_of_tickets_available_func(){ $voyage_id = get_the_ID(); $children = get_posts( array( 'post_type'=> $child_slug, 'meta_query' => array( array( 'key' => '_wpcf_belongs_voyage_id', 'value' => $voyage_id, ), ), 'fields' => 'ids' ) ); $sum = 0; foreach($children as $id){ $sum += get_post_meta( $id, 'wpcf-total-tickets-available', true); } return $sum; }
2) Then in a single "Voyage" post, display the total value with shortcode:
[sum-of-tickets-available]
Relevant Documentation:
https://codex.wordpress.org/Function_Reference/get_posts
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.
Our next available supporter will start replying to tickets in about 1.27 hours from now. Thank you for your understanding.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
- | 9:00 – 13:00 | 9:00 – 13:00 | 9:00 – 13:00 | 9:00 – 13:00 | 9: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/Hong_Kong (GMT+08:00)
This topic contains 3 replies, has 2 voices.
Last updated by 7 years, 1 month ago.
Assisted by: Luo Yang.