Skip Navigation

[Resolved] Sum of custom field of intermediary posts in nested view

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 3 replies, has 2 voices.

Last updated by attilaT 1 month, 3 weeks ago.

Assigned support staff: Waqar.

Author
Posts
#2124551

I have two CPT-s: 'Edzesek' and 'Vendegek'
Between them i have intermediary post type called 'Edzesen resztvevo vendegek' with a custom field 'befizetes'.

I have a view called 'edzes-teljes-bevetel' which is showing a sum of the 'befizetes' fields for EACH 'Edzesek'.

I am trying to make a new view where I show the sum of the befizetes of ALL 'Edzesek'.
Tried this way, but it's not working:

[wpv-layout-start]
[wpv-items-found]
Edzések száma: [wpv-found-count]
<!-- wpv-loop-start -->
<wpv-loop>
[add-to-total]
[wpv-view name="edzes-teljes-bevetel"]
[/add-to-total]
</wpv-loop>
<!-- wpv-loop-end -->
[show-total][/show-total]
[/wpv-items-found]
[wpv-no-items-found]
[wpml-string context="wpv-views"]No items found[/wpml-string]
[/wpv-no-items-found]
[wpv-layout-end]

Is there a way to achive I am trying to do?

#2125007

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting us and I'd be happy to assist.

From your message, I understand you're using a custom shortcode approach, as suggested in this reply:
https://toolset.com/forums/topic/is-there-any-way-to-calculate-sum-of-values-of-prticular-field-in-picked-items/

To troubleshoot why it is not working on your website, I'll need to see how this field and the views are set up, in the admin area.

Can you please share temporary admin login details, along with the link to the page where these views can be seen?

Note: Your next reply will be private and please make a complete backup copy, before sharing the access details.

regards,
Waqar

#2128865

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for waiting and I apologize for the delay in getting back on this.

During testing, I noticed that the total sum of the befizetes linked to individual 'Edzesek' post was showing correctly using the view "Edzés teljes bevétel".

But, to show another sum that calculates the befizetes linked to all the 'Edzesek' posts, you'll need another shortcode similar to the one that you have already registered. I have temporarily added it in the Toolset's custom code area ( at Toolset -> Settings -> Custom Code ) and you can move it to the theme's "functions.php" file later, as needed:


global $grandtotal;
function add_grandtotal_shortcode($atts, $content = '') {
global $grandtotal;
  
$grandtotal += (int)wpv_do_shortcode($content);
}
  
add_shortcode('add-to-grandtotal', 'add_grandtotal_shortcode');
  
function show_grandtotal_shortcode() {
global $grandtotal;
$grandtotalNew = $grandtotal;
$grandtotal = 0;
return $grandtotalNew;
}
  
add_shortcode('show-grandtotal', 'show_grandtotal_shortcode');

Please note that it is a copy of the existing shortcode and I've just appended the word "grand" with each item, to make it unique from the existing one.

Next, in the child view "Edzés teljes bevétel", I updated the loop editor's content to include the field's total using both these shortcodes:


[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
	<wpv-loop>
		[add-to-total]
			[types field='befizetes' format='FIELD_VALUE ft' item='@edzesen-resztvevo-vendeg.intermediary'][/types]
		[/add-to-total]
		[add-to-grandtotal]
			[types field='befizetes' format='FIELD_VALUE ft' item='@edzesen-resztvevo-vendeg.intermediary'][/types]
		[/add-to-grandtotal]
	</wpv-loop>
	<!-- wpv-loop-end -->
	<strong>[wpml-string context="wpv-views"]Bevétel: [/wpml-string]</strong>[show-total][/show-total] ft<br>
	[/wpv-items-found]
[wpv-layout-end]

And in the parent view "Stat - minden edzés", I updated the loop editor's content to included new grandtotal shortcode, like this:


[wpv-layout-start]
	[wpv-items-found]
	Edzések száma: [wpv-found-count]<br>
	<!-- wpv-loop-start -->
		<wpv-loop>
			[wpv-view name="edzes-teljes-bevetel"]
		</wpv-loop>
	<!-- wpv-loop-end -->
	<hr>
	<strong>[wpml-string context="wpv-views"]Bevétel: [/wpml-string]</strong>[show-grandtotal][/show-grandtotal] ft
	[/wpv-items-found]
	[wpv-no-items-found]
		<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
	[/wpv-no-items-found]
[wpv-layout-end]

Now, when you'll see that page with a user who has any 'Edzesek' post(s), you'll see the sum of befizetes linked to an individual as well as all the 'Edzesek' posts.

#2129597

My issue is resolved now.
Works like a charm! 🙂
Thank you Waqar!