我使用get_terms
显示某些类别。我还使用自定义字段_postviews
统计每篇文章的视图。
我想按自定义字段的总和对这些类别进行排序。有什么办法可以做到这一点吗?
我使用get_terms
显示某些类别。我还使用自定义字段_postviews
统计每篇文章的视图。
我想按自定义字段的总和对这些类别进行排序。有什么办法可以做到这一点吗?
$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 );
理论上,上述代码应按以下方式工作它在你的类别中循环
一切都好吗<我需要wp-list-table 也要显示custom-fields 在每个custom-post 我有,但我不知道如何做到这一点,在这幅图中,它显示了带有字段的表格:Title, Author and Publication Date: 我想要的是能够选择custom-fields 将出现,例如以下示例Title, Carta, Naipe, Author, and Date of Publication: