每月为一个仪表板小工具获取多少帖子

时间:2012-10-02 作者:Nicole

我正在尝试开发一个仪表板小部件,它按月统计帖子或帖子类型。我真的不知道该怎么做。

我知道如何创建一个仪表板小部件,以及所有具有post计数、post类型计数的小部件。

我甚至有一个自定义的,列出了我最近的帖子类型。但我还没有创建一个只计算一个月内有多少个帖子或帖子类型的帖子,我只是不知道该怎么做。

任何关于这方面的想法和帮助都会很棒。谢谢。:)

我希望它类似于这样:

This is how i would like it to look like.

这是以下脚本的更新。然而,我无法让它工作。

add_action(\'wp_dashboard_setup\', \'dashboard_test\');

function dashboard_test() {
global $wp_meta_boxes;

wp_add_dashboard_widget(\'month_dashboard\', \'Reports Submitted for the Year\',   \'custom_test\');
}

function custom_test() {
global $wpdb;
$years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts WHERE   post_status = \'publish\' AND post_type = \'report\' ORDER BY post_date DESC");

foreach($years as $year) 

    $months = $wpdb->get_col("SELECT DISTINCT MONTH(post_date) FROM $wpdb->posts WHERE post_status = \'publish\' AND post_type = \'report\' AND YEAR(post_date) = \'".$year."\' ORDER BY post_date DESC");
    
    foreach($months as $month)  

            if ($month < 10)
            {
                $month = \'0\' . $month; 
            } 
            
            $theids = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE post_status = \'publish\' AND post_type = \'report\' AND MONTH(post_date)= \'".$month."\' AND YEAR(post_date) = \'".$year."\' ORDER BY post_date ASC");
            
            echo \'<h4>\';
                    
                        echo \'<li><a href="/report/\'.$year.\'/\'.$month.\'">\' . date( \'F\', mktime(0, 0, 0, $month) ). \' (\' . count($theids) . \')</a></li>\' ;
                   
                echo \'</h4>\';
                
}
我遇到了这个错误:在/var/www/vhosts/hibsterlite中的非对象上调用成员函数get\\u col()。com/httpdocs/wp-content/themes/hibsterlite/functions‌​.php第209行

Ok I no longer get the error above however I only get one month listed atm. Any ideas guys? :)

3 个回复
最合适的回答,由SO网友:Bainternet 整理而成

刚刚检查了您的代码,看起来不错,但您缺少foreach循环的代码块,因此:

add_action(\'wp_dashboard_setup\', \'dashboard_test\');
function dashboard_test() {
    global $wp_meta_boxes;
    wp_add_dashboard_widget(\'month_dashboard\', \'Reports Submitted for the Year\',   \'custom_test\');
}

function custom_test(){
    global $wpdb;
    $years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts WHERE   post_status = \'publish\' AND post_type = \'report\' ORDER BY post_date DESC");
    foreach($years as $year){
        $months = $wpdb->get_col("SELECT DISTINCT MONTH(post_date) FROM $wpdb->posts WHERE post_status = \'publish\' AND post_type = \'report\' AND YEAR(post_date) = \'".$year."\' ORDER BY post_date DESC");
        foreach($months as $month) {
            $month = ($month < 10)? \'0\' . $month : $month; 
            $theids = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE post_status = \'publish\' AND post_type = \'report\' AND MONTH(post_date)= \'".$month."\' AND YEAR(post_date) = \'".$year."\' ORDER BY post_date ASC");
            echo \'<h4>\';
            echo \'<li><a href="/report/\'.$year.\'/\'.$month.\'">\' . date( \'F\', mktime(0, 0, 0, $month) ). \' (\' . count($theids) . \')</a></li>\' ;
            echo \'</h4>\';
        }
    }
}
而且效果很好:

enter image description here

SO网友:Jonathan

我为自定义post类型的归档实现了类似的功能,因为我不喜欢处理wp\\u get\\u归档。基本上,我为一年中的所有帖子创建了一个自定义sql查询,然后按月进行细分,然后每月对帖子进行计数。

<ul>

    <?php

    $years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts WHERE post_status = \'publish\' AND post_type = \'mediaposts\' ORDER BY post_date DESC");

    foreach($years as $year) :

        $months = $wpdb->get_col("SELECT DISTINCT MONTH(post_date) FROM $wpdb->posts WHERE post_status = \'publish\' AND post_type = \'mediaposts\' AND YEAR(post_date) = \'".$year."\' ORDER BY post_date DESC");

            foreach($months as $month) : 

                if ($month < 10)
                {
                    $month = \'0\' . $month; 
                } 

                $theids = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE post_status = \'publish\' AND post_type = \'mediaposts\' AND MONTH(post_date)= \'".$month."\' AND YEAR(post_date) = \'".$year."\' ORDER BY post_date ASC");

                    ?>

                    <h4>
                        <?php 
                            echo \'<li><a href="/mediaposts/\'.$year.\'/\'.$month.\'">\' . date( \'F\', mktime(0, 0, 0, $month) ). \' (\' . count($theids) . \')</a></li>\' ;
                        ?>
                    </h4>
            <?php  

            endforeach;?>

    <?php 

    endforeach;

    ?>

</ul>
这就是最终结果:

Custom Query end result

SO网友:Michael Ecklund

我会使用更多的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)每次生成报告时,都需要在每个自定义分类中自动设置适当的术语。这可以使用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\');因此,当您单击报告计数时,它会将您带到一个仅包含当年、月或日生成的报告的表,并将按报告标题升序排序。

结束

相关推荐

About Hooks and Filters

嗯,我很难理解动作和过滤器之间的区别。我确实在代码中使用动作,但我是一个新手,甚至连一点过滤器都不知道。我去过codex,以及NickTheGeek、BillErickson、GaryJones等的多个网站,但没有去过vein。如果你能用简单的话告诉我,并举例说明动作、过滤器和挂钩的基本内容和区别。非常感谢。