从每个类别的职位,并责令其词preSS总和meta_values [英] Wordpress Sum meta_values from posts and order them per category

查看:170
本文介绍了从每个类别的职位,并责令其词preSS总和meta_values的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这个code查询所有从数组的类别和每个类别求和meta_key值:

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

和它工作正常。但我不能只显示前5个类别,有custom_value最总和。请你帮我出这一点。

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.

太感谢了。

推荐答案

最后,与谷歌的一点点我得到它: - ):

Finally with a little bit of google i got it :-):

<? $totalvotes = get_meta_values( 'proyecto_votos', 'proyecto' ); ?>    
<? 
foreach ($arr_cat_reg 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;       
    //echo $MySum.'<br/>';              
    $percent = $MySum * 100;
    $percent = $percent / $totalvotes;              
    //echo $percent;                
    wp_reset_postdata();
    $catslug = get_cat_slug($cat);
    $most_voted[] = array('region' => $catslug, 'votos' => $MySum); 
}               
$sortArray = array();

foreach($most_voted as $region){
    foreach($region as $key=>$value){
        if(!isset($sortArray[$key])){
            $sortArray[$key] = array();
        }
        $sortArray[$key][] = $value;
    }
} 
$orderby = "votos";
array_multisort($sortArray[$orderby],SORT_DESC,$most_voted); 
$top5 = array_slice($most_voted, 0, 5);         
?>              

我希望这可以帮助别人。

I Hope this helps somebody.

这篇关于从每个类别的职位,并责令其词preSS总和meta_values的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆