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?

  • 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 Aug 30 '16 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 Aug 31 '16 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 Aug 31 '16 at 10:17
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 );
  • 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 Sep 1 '16 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 Sep 1 '16 at 11:33
  • can you guide me how can I use WP_Cache? – ma_dev_15 Sep 22 '16 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 Sep 22 '16 at 21:20
1

You can try this code.

$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));

<?php echo $all_downloads; ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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