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:
- Grab the posts you need
- Get their meta values for
amount_to_paid
usingget_post_meta
- 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 );