EDIT 2
下面是编辑1中的另一个版本的代码。这段代码要快得多。这是我在编辑1和编辑2中的代码之间的测试
编辑1数据库查询时间=+/-0.25,数据库查询=69
编辑2数据库查询时间=+/-0.07,数据库查询=29
这是代码
<?php
$oldest = get_posts( \'post_type=post&post_status=publish&posts_per_page=1&order=ASC\' );
$oldest_date = $oldest[0]->post_date;
$first_date = date(\'Y\', strtotime($oldest_date));
$todays_date = date(\'Y\');
$year_range = range($todays_date, $first_date);
foreach ($year_range as $year) { // dynamic year-based tables
echo \'<h2>\' . $year . \'</h2>\';
$terms = get_terms(\'category\');
$term_slugs = array();
if ( !empty( $terms ) && !is_wp_error( $terms ) ) { // table body
foreach ( $terms as $key=>$term){
$term_slugs[$key] = $term->slug;
}
echo \'
<table class="statistics">
<tbody>
\';
echo \'
<thead>
<tr>
<td>Taxonomy Term</td>
<td>Percentage</td>
<td class="chart-count">Count</td>
</tr>
</thead>
\';
$posts_count = array(); // Holds all term post counts in an array
$terms_array = array(); // Holds all term names in an array
$args = array(
\'posts_per_page\' => -1,
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
\'year\' => $year,
\'tax_query\' => array(
array(
\'taxonomy\' => \'category\',
\'field\' => \'slug\',
\'terms\' => $term_slugs,
\'include_children\' => false
),
),
);
$yearly_posts_per_term = new WP_Query($args);
$posts_count[] = $yearly_posts_per_term->post_count; //Collects post counts and send them to an array
if($yearly_posts_per_term->have_posts()):
while($yearly_posts_per_term->have_posts()): $yearly_posts_per_term->the_post();
$terms = get_the_terms( $post->ID, \'category\' );
if ( $terms && ! is_wp_error( $terms ) ) {
foreach ( $terms as $term ) {
$terms_array[] = $term->slug;
}
}
endwhile;
endif;
}
$total_posts = array_sum($posts_count); //Use array_sum to add up all the separate post counts
$result = array_count_values($terms_array);
foreach ($result as $term_name=>$count) {
$percentage = round( (($count / $total_posts)*100), 2 ); //Calculate the percentages of each term post cound to total year post count
echo \'
<tr>
<td class="chart-item">\'.$term_name.\'</td>
<td class="chart-visual"><div class="chart-bar" style="width:\'.$percentage.\'%;"></div> \'.$percentage.\'%</td>
<td class="chart-count">\'.$count.\'</td>
</tr>
\';
}
echo \'
<tfoot>
<tr>
<td colspan="2">Posts total</td>
<td class="chart-count">\'.$total_posts.\'</td>
</tr>
</tfoot>
\';
echo \'
</tbody>
</table>
\';
} // end of year-based list
?>
这将呈现与“编辑1”中的表格相同的输出,只是它不显示空术语,只显示带有帖子的术语
EDIT 1
根据您编辑的问题,这里是新代码的精简部分。我不得不在这里废弃一到两件东西,重新安排一些元素,以使其工作。这里最大的挑战是计算百分比,因为用于计算百分比的变量是独立的
foreach
循环。内的变量
foreach
循环仅存在于其中
foreach
, 不超过
您的编辑对代码(来自我的原始答案,@deflame code和您的集成代码)的重大更改是
将包含职位总数、百分比和术语名称的两个表格移至$terms
foreach
环
将术语名称和帖子数从每个术语推送到$terms
foreach
环
去除石灰代码时报废,已删除$total_posts = 0;
仅保留和修改$percentage = round( (($yearly_posts_per_term->post_count / $total_posts)*100), 2 );
已使用array_sum
从每个学期的职位计数数组中获取当年的职位总数
已使用array_combine
使用每个术语的术语名称和post计数创建关联数组的步骤
最后,我用了foreach
循环以获取每个术语名称和相关的post计数,并将其反馈到表中
这是最后的代码
<?php
$oldest = get_posts( \'post_type=post&post_status=publish&posts_per_page=1&order=ASC\' );
$oldest_date = $oldest[0]->post_date;
$first_date = date(\'Y\', strtotime($oldest_date));
$todays_date = date(\'Y\');
$year_range = range($todays_date, $first_date);
foreach ($year_range as $year) { // dynamic year-based tables
echo \'<h2>\' . $year . \'</h2>\';
$terms = get_terms(\'category\');
if ( !empty( $terms ) && !is_wp_error( $terms ) ) { // table body
echo \'
<table class="statistics">
<tbody>
\';
echo \'
<thead>
<tr>
<td>Taxonomy Term</td>
<td>Percentage</td>
<td class="chart-count">Count</td>
</tr>
</thead>
\';
$posts_count = array(); // Holds all term post counts in an array
$term_names = array(); // Holds all term names in an array
foreach($terms as $term) {
$term_names[] = $term->name; //Collects term names and send them to an array
$args = array(
\'posts_per_page\' => -1,
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
\'year\' => $year,
\'tax_query\' => array(
array(
\'taxonomy\' => \'category\',
\'field\' => \'slug\',
\'terms\' => $term->slug,
\'include_children\' => false
),
),
);
$yearly_posts_per_term = new WP_Query($args);
$posts_count[] = $yearly_posts_per_term->post_count; //Collects post counts and send them to an array
} // endforeach
unset($term);
}
$total_posts = array_sum($posts_count); //Use array_sum to add up all the separate post counts
$combine = array_combine($term_names,$posts_count); //Use array_combine to combine term names and post counts into assosiative array
foreach ($combine as $term_name=>$count) {
$percentage = round( (($count / $total_posts)*100), 2 ); //Calculate the percentages of each term post cound to total year post count
echo \'
<tr>
<td class="chart-item">\'.$term_name.\'</td>
<td class="chart-visual"><div class="chart-bar" style="width:\'.$percentage.\'%;"></div> \'.$percentage.\'%</td>
<td class="chart-count">\'.$count.\'</td>
</tr>
\';
}
echo \'
<tfoot>
<tr>
<td colspan="2">Posts total</td>
<td class="chart-count">\'.$total_posts.\'</td>
</tr>
</tfoot>
\';
echo \'
</tbody>
</table>
\';
} // end of year-based list
?>
请注意,与我原来的回答一样,我将帖子类型改为
post
和分类法
category
用于测试目的。
您的最终结果是一个如下所示的表<请注意,我所有的学期名称都是用南非荷兰语命名的,因为我在我的测试网站上测试的是用南非荷兰语命名的。
ORIGINAL ANSWER
这是我关于如何做到这一点的想法的一个非常粗略的草稿。我没有包含HTML标记,并且使用了默认的帖子类型
post
以及内置分类法
category
测试代码。
下面是我如何构造完整查询的
首先,获取网站上最早的帖子(应该是第一篇帖子)的日期。这是通过一个简单的get_posts
查询修改以满足您的需要
$oldest = get_posts( \'post_status=publish&posts_per_page=1&order=ASC\' );
$oldest_date = $oldest[0]->post_date;
接下来,去掉返回的日期,只获取从发布日期算起的年份。使用
strtotime()
函数将年份转换为Unix时间戳
$first_date = date(\'Y\', strtotime($oldest_date));
返回当前日期,您只需要年份。使用
date()
作用
$current_date = date(\'Y\');
将两个日期返回到
range()
用于打印两个日期之间的年份范围的函数
$year_range = range($current_date, $first_date);
将这些范围添加回
foreach loop
在基于年份的列表中获取您的帖子
我用过get_terms()
获取有关分类法的所有可用术语的列表
$terms = get_terms(\'category\');
现在,所有这些信息都需要反馈到
tax_query
使用
WP_Query
$args = array(
\'posts_per_page\' => -1,
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
\'year\' => $year,
\'tax_query\' => array(
array(
\'taxonomy\' => \'category\',
\'field\' => \'slug\',
\'terms\' => $term->slug
),
),
);
$posts = new WP_Query($args);
最后,您要返回术语名称和每个术语的帖子数量
echo $term->name . \'(\' . $posts->post_count . \')\';
现在大家一起!!
<?php
$oldest = get_posts( \'post_status=publish&posts_per_page=1&order=ASC\' );
$oldest_date = $oldest[0]->post_date;
$first_date = date(\'Y\', strtotime($oldest_date));
$current_date = date(\'Y\');
$year_range = range($current_date, $first_date);
foreach ($year_range as $year) {
echo $year;
$terms = get_terms(\'category\');
if ( !empty( $terms ) && !is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
$args = array(
\'posts_per_page\' => -1,
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
\'year\' => $year,
\'tax_query\' => array(
array(
\'taxonomy\' => \'category\',
\'field\' => \'slug\',
\'terms\' => $term->slug
),
),
);
$posts = new WP_Query($args);
echo $term->name . \'(\' . $posts->post_count . \')\';
}
}
}
?>
如前所述,这是可以改进的,所以采用这个想法和代码,并根据您的需要进行调整和修改。希望这有帮助。