I’m using this code to query all the categories from an array and sum the meta_key values per category:
<?
$arr_cat = array(1,34,64,32);
foreach ($arr_cat as $cat) {
$MySum = 0;
$args = array(
'cat' => $cat,
'meta_key' => 'proyecto_votos',
'post_type' => 'proyecto',
'posts_per_page' => '-1');
$the_query = new WP_Query( $args);
while ( $the_query->have_posts() ) : $the_query->the_post();
$MySum += get_post_meta($post->ID, 'proyecto_votos', true);
endwhile;
wp_reset_postdata();
}
//var_dump($arr_cat);
?>
And it works ok. But I can’t show only the top 5 categories with most sum of custom_value. Please can you help me out on this.
Thanks so much.
Only for top 5 posts
posts_per_page => -1
will show all posts.Reference.
Finally with a little bit of google i got it :-):
I Hope this helps somebody.