0

Here is my query args :

 $query_args = array(
            'post_type' => 'rented_properties',
            'post_status' => 'publish',
            'order' => 'DESC',
            // 'fields' => 'SUM(amount_to_paid)',
        );


        $my_querys = null;
        $my_querys = new WP_Query($query_args);

My meta key is amount_to_paid, and I want to get the sum all the meta_values of this query condition. Please suggest your answer with query_args.

My Scenario : I have 1000 posts and each with meta_key amount_to_paid and it has some value. Now I filter 20 posts and want to get sum of meta_value of amount_to_paid . Did you got me now?

3
  • This is the wrong way to go about this, you should be calculating and updating this value whenever an amount_to_paid is added/changed, WP_Query retrieves posts, and that's all it does. It won't do calculations for you and it isn't SQL, you need to do those parts using the information WP_Query returns. Think of it as WP_Post_Query rather than WP_SQL_Query
    – Tom J Nowell
    Commented Aug 30, 2016 at 14:07
  • Dear Tom, I have 1000 posts and each with meta_key amount_to_paid and it has some value. Now I filter 20 posts and want to get sum of meta_value of amount_to_paid . Did you got me now?
    – ma_dev_15
    Commented Aug 31, 2016 at 7:25
  • I understand, but WP_Query won't calculate that for you, you need to grab the posts, then do the adding in an additional step, there's no argument you can add to WP_Query to get an answer from it
    – Tom J Nowell
    Commented Aug 31, 2016 at 10:17

2 Answers 2

1

WP_Query gets posts from the database, but it's not a generic SQL query class, it should really be called WP_Post_Query, as WP_Query implies it can do any SQL.

As a result, you need to do several things:

  1. Grab the posts you need
  2. Get their meta values for amount_to_paid using get_post_meta
  3. Add those values up using the basic PHP maths + - / * = += -=

So:

$sum = 0;
$query = new WP_Query($query_args);
if ( $query->have_posts() ) {
    while( $query->have_posts() ) {
        $query->the_post();
        // do the processing for each post
        $sum = $sum + get_post_meta( ... );
    }
}
echo esc_html( $sum );
6
  • I know we can do like this, but I think SQL query exectution is much much faster that PHP while loops for adding values. What you say?
    – ma_dev_15
    Commented Sep 1, 2016 at 4:19
  • These values all get cached by the internal WP_Cache system, so any other code that uses them will be sped up. Assuming you have an object store such as redis or memcache configured and setup. You should use the APIs and avoid raw SQL queries when possible
    – Tom J Nowell
    Commented Sep 1, 2016 at 11:33
  • can you guide me how can I use WP_Cache?
    – ma_dev_15
    Commented Sep 22, 2016 at 2:48
  • In this case it's all happening behind the scenes, but if you want to store your own values, I'd suggest asking another question as it's a new subject. It's not difficult though, and a quick google will reveal the 2 basic functions you need
    – Tom J Nowell
    Commented Sep 22, 2016 at 21:20
  • Where do you specify the post type?
    – Amjad
    Commented Feb 8, 2021 at 23:43
4

You can try this code.

global $wpdb;
$meta_key = 'link_click_counter';
$all_downloads = $wpdb->get_var($wpdb->prepare("
                                  SELECT sum(meta_value) 
                                  FROM $wpdb->postmeta 
                                  WHERE meta_key = %s", $meta_key));

echo $all_downloads;
3
  • I can confirm this works as long as you declare global $wpdb. Commented Sep 9, 2020 at 17:00
  • The weakness/error here however is that you are counting values for all posts --- all post types, draft or published, public or private --- and cannot sort the unwanted out as in the accepted (WP_query + loop) answer... So the OP has to adapt this answer by joining the table $wpdb->postmeta to $wpdb->posts, so the WHERE clause gets AND post_type = 'rented_properties' (and maybe also AND post_status = 'published'. Commented Sep 9, 2020 at 17:08
  • This should be the accepted answer Commented Oct 4, 2022 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.