WP_Query order by the sum total of a postmeta value

360 Views Asked by At

I am trying to create a custom query to order the posts by the sum of the values for the '_heateor_sss_shares_meta' key in the postmeta table. A sample of the values that I am working with is:

a:5:{s:8:"facebook";i:0;s:7:"twitter";i:0;s:6:"reddit";i:0;s:8:"linkedin";i:0;s:9:"pinterest";i:1;}

What I understand that I should do to get the sum of the values for that record is:

$share_counts = get_post_meta( $post_id, '_heateor_sss_shares_meta', true );

The WP_Query code that I need to amend with the orderby is:

$args=array(
    'post_type' => "listing",
    'post_status' => 'publish',
    'posts_per_page' => $number_posts,
);

What I can gather is that I need to add:

'meta_key' => '_heateor_sss_shares_meta',
'orderby' => 'meta_value_num',     <<<<<<<<<<<<<<< I don't know what should go here to do the calculation
'order' => 'DESC'

My question is, how can I do a sum calculation on the value of the '_heateor_sss_shares_meta' key so that I can order by the highest to the lowest?

0

There are 0 best solutions below