LoginSignup
28
29

More than 5 years have passed since last update.

【WP-Champloo】wp_get_archivesを配列で取得する関数

Last updated at Posted at 2012-12-26

標準テンプレータグでは整形されたHTMLコードが出力される為、配列を返す関数を作ってみました。

ソースコード

functions.php
<?php
/**
* @function get_archives_array
* @param post_type(string) / period(string) / year(Y) / limit(int)
* @return array
*/
if(!function_exists('get_archives_array')){
    function get_archives_array($args = ''){
        global $wpdb, $wp_locale;

        $defaults = array(
            'post_type' => '',
            'period'  => 'monthly',
            'year' => '',
            'limit' => ''
        );
        $args = wp_parse_args($args, $defaults);
        extract($args, EXTR_SKIP);

        if($post_type == ''){
            $post_type = 'post';
        }elseif($post_type == 'any'){
            $post_types = get_post_types(array('public'=>true, '_builtin'=>false, 'show_ui'=>true));
            $post_type_ary = array();
            foreach($post_types as $post_type){
                $post_type_obj = get_post_type_object($post_type);
                if(!$post_type_obj){
                    continue;
                }

                if($post_type_obj->has_archive === true){
                    $slug = $post_type_obj->rewrite['slug'];
                }else{
                    $slug = $post_type_obj->has_archive;
                }

                array_push($post_type_ary, $slug);
            }

            $post_type = join("', '", $post_type_ary); 
        }else{
            if(!post_type_exists($post_type)){
                return false;
            }
        }
        if($period == ''){
            $period = 'monthly';
        }
        if($year != ''){
            $year = intval($year);
            $year = " AND DATE_FORMAT(post_date, '%Y') = ".$year;
        }
        if($limit != ''){
            $limit = absint($limit);
            $limit = ' LIMIT '.$limit;
        }

        $where  = "WHERE post_type IN ('".$post_type."') AND post_status = 'publish'{$year}";
        $join   = "";
        $where  = apply_filters('getarchivesary_where', $where, $args);
        $join   = apply_filters('getarchivesary_join' , $join , $args);

        if($period == 'monthly'){
                $query = "SELECT YEAR(post_date) AS 'year', MONTH(post_date) AS 'month', count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC $limit";
        }elseif($period == 'yearly'){
            $query = "SELECT YEAR(post_date) AS 'year', count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date) ORDER BY post_date DESC $limit";
        }

        $key = md5($query);
        $cache = wp_cache_get('get_archives_array', 'general');
        if(!isset($cache[$key])){
            $arcresults = $wpdb->get_results($query);
            $cache[$key] = $arcresults;
            wp_cache_set('get_archives_array', $cache, 'general');
        }else{
            $arcresults = $cache[$key];
        }
        if($arcresults){
            $output = (array)$arcresults;
        }

        if(empty($output)){
            return false;
        }

        return $output;
    }
}
?>

パラメータ

post_type(投稿タイプを指定)

  • post(投稿)初期値
  • any(全てのカスタム投稿タイプ)

period(アーカイブ種別を指定)

  • monthly(月別アーカイブ)初期値
  • yearly(年別アーカイブ)

year(期間を指定)

  • 2012(西暦を4桁で示す年)

limit(取得するアーカイブ記事の数を指定)

  • 表示件数(正数)

使用例

年別アーカイブリストを取得したい場合

ソース
<?php $archives = get_archives_array(array('period'=>'yearly')); ?>
<?php if($archives): ?>
<ul>
<?php foreach($archives as $archive): ?>
<li><a href="<?php echo get_year_link($archive->year); ?>"><?php echo $archive->year; ?>年(<?php echo $archive->posts; ?>)</a></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
出力
<ul>
<li><a href="http://xxxx.xxx/2013/">2013年(10)</a></li>
<li><a href="http://xxxx.xxx/2012/">2012年(1)</a></li>
<li><a href="http://xxxx.xxx/2011/">2011年(3)</a></li>
</ul>

月別アーカイブリストを取得したい場合

ソース
<?php $archives = get_archives_array(); ?>
<?php if($archives): ?>
<ul>
<?php foreach($archives as $archive): ?>
<li><a href="<?php echo get_month_link($archive->year, $archive->month); ?>"><?php echo $archive->year; ?><?php echo $archive->month; ?>月(<?php echo $archive->posts; ?>)</a></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
出力
<ul>
<li><a href="http://xxxx.xxx/2013/04/">2013年4月(10)</a></li>
<li><a href="http://xxxx.xxx/2012/12/">2012年12月(1)</a></li>
<li><a href="http://xxxx.xxx/2011/10/">2011年10月(3)</a></li>
</ul>

期間指定の月別アーカイブリストを取得したい場合

ソース
<?php $archives = get_archives_array(array('year'=>2013)); ?>
<?php if($archives): ?>
<ul>
<?php foreach($archives as $archive): ?>
<li><a href="<?php echo get_month_link($archive->year, $archive->month); ?>"><?php echo $archive->year; ?><?php echo $archive->month; ?>月(<?php echo $archive->posts; ?>)</a></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
出力
<ul>
<li><a href="http://xxxx.xxx/2013/04/">2013年4月(6)</a></li>
<li><a href="http://xxxx.xxx/2013/03/">2013年3月(1)</a></li>
<li><a href="http://xxxx.xxx/2013/01/">2013年1月(3)</a></li>
</ul>

カスタム投稿タイプの月別アーカイブリストを取得したい場合

ソース
<?php $archives = get_archives_array(array('post_type'=>'カスタム投稿タイプ名')); ?>
<?php if($archives): ?>
<ul>
<?php foreach($archives as $archive): ?>
<?php print_r($archive); ?>
<li><a href="<?php echo get_post_type_month_link('カスタム投稿タイプ名', $archive->year, $archive->month); ?>"><?php echo $archive->year; ?><?php echo $archive->month; ?>月(<?php echo $archive->posts; ?>)</a></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
出力
<ul>
<li><a href="http://xxxx.xxx/2013/04/カスタム投稿タイプ名/">2013年4月(3)</a></li>
<li><a href="http://xxxx.xxx/2013/03/カスタム投稿タイプ名/">2013年3月(2)</a></li>
</ul>
  • 上記出力に関して「get_post_type_month_link」関数は存在しない為、WP-Champlooプラグインのインストールをお勧めします。
28
29
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
28
29