WordPressで直近30日の投稿数ランキングを表示する

下記のコードを外観->テーマの編集->functons.phpに追加すると月間投稿数ランキングのウィジェットが使用可能になります。

class CountAllUserPosts extends WP_Widget {
    function CountAllUserPosts () {
        parent::WP_Widget(false, $name = '月間投稿数ランキング');
    }
    function widget($args, $instance) {
        extract( $args );
        $title = apply_filters( 'widget_title', $instance['title'] );
        $body = apply_filters( 'widget_body', $instance['body'] );
        ?>
        <?php echo $before_widget; ?>
        <?php if ( $title ) echo $before_title . $title . $after_title; ?>
        <?php $rank = @$this->count_all_users_posts(); ?>
        <!-- BeginPostsRanking -->
        <h3 class="widgettitle">月間投稿数ランキング</h3>
        <ul>
        <?php $index=0; foreach ($rank as $id=>$count) { $index++;$_user = get_user_by('id', $id);?>
            <li>
                <span class="rank"><?php echo $index;?>.</span>
                <a href="/members/<?php echo $_user->get('user_nicename');?>" >
                    <?php echo $_user->get('display_name');?>
                </a><span class="contents_count"> - <?php echo $count;?> hacks</span>
            </li>
        <?php }?>
        </ul>
        <!-- EndPostsRanking -->
        <?php echo $after_widget; ?>
        <?php
    }
    function update($new_instance, $old_instance) {
    return $new_instance;
    }
    function form($instance) {
      
    }
    
    function count_all_users_posts( $post_type = 'post', $public_only = true ) {
        global $wpdb;

        $count = array();
        $userlist = implode( ',', array_map( 'absint', $users ) );
        $where = get_posts_by_author_sql( $post_type, true, null, $public_only );
        $from_date = date('Y-m-d H:i:s', strtotime("-30 days"));
        $where .= " AND post_date > '${from_date}'";
        $result = $wpdb->get_results( "SELECT post_author, COUNT(*) as cnt FROM $wpdb->posts $where AND post_author GROUP BY post_author ORDER BY cnt DESC LIMIT 10", ARRAY_N );
        foreach ( $result as $row ) {
            $count[ $row[0] ] = $row[1];
        }

        foreach ( $users as $id ) {
            if ( ! isset( $count[ $id ] ) )
                $count[ $id ] = 0;
        }

        return $count;
    }
}
add_action('widgets_init', create_function('', 'return register_widget("CountAllUserPosts");'));