Skip Navigation

[Résolu] Calculate average from custom field

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

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 5 réponses, has 2 voix.

Last updated by robertH-3 Il y a 9 années et 5 mois.

Assigned support staff: Luo Yang.

Auteur
Publications
#184591

I'm Using a (Gravity) Form to create Custom Posts 'Bedrijfsanalyses'. The Custom Post Type contains custom fields, created with Types. I want to calculate the average from certain custom fields, calculated from ALL Custom Post Types 'Bedrijfsanalyses that contain that certain custom field.

What I have found in the Forum is that this isn't possible with Views without some extra coding. I have to create a function and shortcode to do the job, but I didn't find how to do this. Could You please help me?

#184676

Luo Yang
Supporter

Languages: Anglais (English ) Chinois simplifié (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

Hi robertH-3

I assume your custom fields is created with Types, which is using slug "certain-custom-field", in database the meta-key is "wpcf-certain-custom-field"
Please try this:
1) add codes in your theme/functions.php

function my_average_func($atts, $content) {
    extract(shortcode_atts(array(
        'slug' => 'wpcf-certain-custom-field',
    ), $atts));
    global $wpdb;
	$res = $wpdb->get_var( "SELECT AVG(`meta_value`) FROM $wpdb->postmeta WHERE `meta_key` = '$slug'" );
    return $res;
}
add_shortcode( 'my_average', 'my_average_func' );

2) put the shotcode into your content: [my_average]

More help:
http://codex.wordpress.org/Function_Reference/add_shortcode

#184696

Hi luoy,

Thanx for the code. I'll have to mannualy check the calculation, but so far so good. Still one more question: is there a way to use the shortcode for calculating averages of other custom fields too? I know I could use this code with another slug to calculate the average for another custom field and so on, but this seems a bit bloated. Is it possible to write a shortcode witch I could use for more than one custom field? I saw that somewhere on this forum, but i can't find it anymore.

Regards,

Robert

#184699

I've checked the calculation. I have six custom posts, but the code calculates the average based on seven items. There's no custom post in the wast basked... What could go wrong here?

#185057

Luo Yang
Supporter

Languages: Anglais (English ) Chinois simplifié (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

1) yes, you can use it for other custom fields, like this:
[my_average slug="other-field-slug"]

2) I guess there is some record with empty value in your database, please check it with mysql tools, for example phpmyadmin, run following sql query:

SELECT * FROM wp_postmeta where `meta_key` = 'wpcf-certain-custom-field';

Check if there is a record with empty value

And if you do not count in the record with empty value, please modify the php codes in your theme/functions.php
as this:

function my_average_func($atts, $content) {
	extract(shortcode_atts(array(
		'slug' => 'wpcf-certain-custom-field',
	), $atts));
	global $wpdb;
	$res = $wpdb->get_var( "SELECT AVG(`meta_value`) FROM $wpdb->postmeta WHERE `meta_key` = '$slug' AND `meta_value` !=''" );
	return $res;
}
add_shortcode( 'my_average', 'my_average_func' );
#185102

Thanx luoy, both issues are resolved now. Prior to this question I did some experimenting with the custom post types and made a normal post with all the custom field in it. That was the 7th post with all the empty values in it.