$terms = get_terms( array(
\'taxonomy\' => \'category\',
\'hide_empty\' => false,
) );
$sort = array();
//loop to list through all category
foreach ( $terms as $term ) {
$id = $term->ID;
$name = $term->name;
$args = array(
\'post_type\' => \'post\',
\'category\' => $name,
);
$query = new WP_Query( $args );
$sum = 0;
if ($query->have_posts() ):while ($query->have_posts() ):$query->the_post();
// to add all the values of the custom field
$sum += get_post_meta( get_the_ID(), \'_postviews \', true );
endwhile;
//adds term name and the total sum of all _postviews meta in array the term name as key abnd the sum as its value
$sort[ $name ] = $sum;
endif;
//sort keys based on values HIGH TO LOW
}
arsort( $sort );
print_r( $sort );
理论上,上述代码应按以下方式工作
它在你的类别中循环
根据你的类别获取帖子获取帖子的最新元数据并存储在变量和中添加它们一旦所有帖子都已检查并存储在变量和中,它们现在存储为变量$排序中的数组请告诉我此代码是否适用于你