我会使用更多的PHP而不是SQL。这样,您只需为您的帖子点击一次数据库。
添加新的WordPress仪表板小部件。
/* Add a Dashboard widget for reports submitted. */
function mbe_add_dashboard_widget(){
wp_add_dashboard_widget(\'mbe_dashboard_widget_reports\', \'Reports Submitted\', \'mbe_dashboard_widget_reports\');
}
add_action(\'wp_dashboard_setup\', \'mbe_add_dashboard_widget\');
为所有提交的报告设置实际的WordPress仪表板小部件。
/* Reports Submitted Widget. */
function mbe_dashboard_widget_reports(){
$data = mbe_get_reports();
if($data){
echo \'<table style="width: 100%;">\'.PHP_EOL;
echo \'<tr>\'.PHP_EOL;
echo \'<td style="width: 33%;"><strong>Reports by Year</strong></td>\'.PHP_EOL;
echo \'<td style="width: 33%;"><strong>Reports by Month</strong></td>\'.PHP_EOL;
echo \'<td style="width: 33%;"><strong>Reports by Day</strong></td>\'.PHP_EOL;
echo \'</tr>\'.PHP_EOL;
echo \'<tr>\'.PHP_EOL;
echo \'<td style="vertical-align: text-top;">\'.mbe_reports_by(\'year\', $data).\'</td>\'.PHP_EOL;// Yearly
echo \'<td style="vertical-align: text-top;">\'.mbe_reports_by(\'month\', $data).\'</td>\'.PHP_EOL;// Monthly
echo \'<td style="vertical-align: text-top;">\'.mbe_reports_by(\'day\', $data).\'</td>\'.PHP_EOL;// Daily
echo \'</tr>\'.PHP_EOL;
echo \'</table>\'.PHP_EOL;
}
}
用于获取提交报告的初始WordPress post数据的主要功能。
/* Get all reports. */
function mbe_get_reports(){
global $wpdb;
/* Prevent typos later on. */
$post_type = \'report\';
$post_status = \'publish\';
$posts = $wpdb->get_results($wpdb->prepare(
"
SELECT ID, post_title, post_date
FROM {$wpdb->posts}
WHERE `post_status` = \'%s\'
AND `post_type` = \'%s\'
",
$post_status,
$post_type
));
/* No posts. */
if(empty($posts)){
return false;
}
$post_data = array(); // Store data for later.
for($i = 0; $i < count($posts); $i++){
$post = $posts[$i];
/* Separate the date from the time. */
$post_date = explode(\' \', $post->post_date);
/* Separate the day, month, and year from each other. */
$the_date = explode(\'-\', $post_date[0]);
$month = date(\'F\', mktime(0, 0, 0, $the_date[1]));
$day = date(\'l\', mktime(0, 0, 0, $the_date[2]));
/* Store post data nested in the format: year, month, day. */
$post_data[$the_date[0]][$month][$day][] = array(
\'post_id\' => $post->ID,
\'post_title\' => $post->post_title,
\'post_slug\' => sanitize_title($post->post_title),
\'post_permalink\' => get_permalink($post->ID),
\'post_type\' => $post_type,
\'post_status\' => $post_status,
\'post_date\' => date(\'l, F d, Y\', strtotime(join(\'-\', $the_date))),// Ex. Saturday, October 06, 2012
\'post_time\' => date(\'g:i A\', strtotime($post_date[1]))// Ex. 3:00 PM
);
}
/* Done with $posts, remove it. */
unset($posts);
/* Return the data. */
if(!empty($post_data)){
$return = $post_data;
} else{
$return = false;
}
return $return;
}
按年、月和日对报告进行排序,然后在WordPress仪表板小部件的表格中以无序列表的形式显示它们。
/* Display report counts by year, month, or day. */
function mbe_reports_by($span, $data){
$post_data = array();
foreach($data as $year => $year_data){
foreach($year_data as $month => $month_data){
foreach($month_data as $day => $day_data){
for($i = 0; $i < count($day_data); $i++){
$post = $day_data[$i];
if($span == \'year\'){
$post_data[$year][] = $post;
} elseif($span == \'month\'){
$post_data[$month][] = $post;
} elseif($span == \'day\'){
$post_data[$day][] = $post;
}
}
}
}
}
if(!empty($post_data)){
$content = \'\';
$content .= \'<ul>\'.PHP_EOL;
uksort($post_data, \'mbe_date_sort\');
foreach($post_data as $time_span => $posts){
$content .= \'<li>\'.$time_span.\' (<a title="View posts in \'.$time_span.\'" href="\'.admin_url(\'/edit.php?post_type=report\', \'http\').\'">\'.count($posts).\'</a>)</li>\'.PHP_EOL;
}
$content .= \'</ul>\'.PHP_EOL;
$return = $content;
} else{
$content = \'<p>No reports found.</p>\'.PHP_EOL;
}
return $content;
}
应该对日期进行排序,而不是将日期杂乱无章地按随机顺序排列。我们不能只按升序或降序排列日期,我们需要按时间顺序排列。
/* Sort dates chronologically. */
function mbe_date_sort($a, $b){
if($a == $b){
return 0;
} else{
$a = strtotime($a);
$b = strtotime($b);
if($a < $b){
return -1;
} else{
return 1;
}
}
}
如果您想超链接所有报表计数,以列出当年、月或日的所有帖子。。。我建议将三种自定义分类法附加到您的“report”自定义帖子类型。
您需要为“按年报告”、“按月报告”和“按日报告”自定义分类法,并且需要将它们附加到您的“报告”自定义帖子类型。你可以用register_taxonomy();
然后,您需要用术语填充每个新创建的分类法。每个术语将反映其各自的分类法。
“reports\\u by\\u year”自定义分类法需要包含生成报告的每一年的术语(例如:2009、2010、2011、2012)“reports\\u by\\u month”自定义分类法需要包含一年中每个月的术语(例如:1月到12月)“reports\\u by\\u day”自定义分类法需要包含一周中每一天的术语(例如:周日到周六)每次生成报告时,都需要在每个自定义分类中自动设置适当的术语。这可以使用wp_set_object_terms();
以及save_post
行动挂钩。了解更多有关WordPress actions.
一旦处理完毕,您可以将其用作报告计数的超链接。
链接到当年的报告:
admin_url(\'/edit.php?reports_by_year=\'.$time_span.\'&post_type=report&orderby=title&order=asc\',
\'http\');
链接到当月的报告:
admin_url(\'/edit.php?reports_by_month=\'.$time_span.\'&post_type=report&orderby=title&order=asc\',
\'http\');
链接到当天的报告:
admin_url(\'/edit.php?reports_by_day=\'.$time_span.\'&post_type=report&orderby=title&order=asc\',
\'http\');
因此,当您单击报告计数时,它会将您带到一个仅包含当年、月或日生成的报告的表,并将按报告标题升序排序。