fork download
  1. function my_get_year_archives( $args = '' ) {
  2.     global $wpdb, $wp_locale;
  3.   
  4.     $defaults = array(
  5.         'date_field' => 'date',
  6.         'format' => 'html',
  7.         'echo' => true,
  8.         'limit' => '',
  9.         'before' => '',
  10.         'after' => '',
  11.         'show_post_count' => true,
  12.     );
  13.   
  14.     $r = wp_parse_args( $args, $defaults );
  15.     extract( $r, EXTR_SKIP );
  16.   
  17.     if ( '' != $limit ) {
  18.         $limit = absint( $limit );
  19.         $limit = ' LIMIT '.$limit;
  20.     }
  21.   
  22.     $field  = 'm.meta_value';
  23.     $select = "SELECT SUBSTRING($field,1,4) AS `year`, SUBSTRING($field,6,2) AS `month`, SUBSTRING($field,9,2) AS `day`, count(p.ID) AS posts";
  24.     $where  = "WHERE p.post_type = 'カスタム投稿名' AND p.post_status = 'publish'";
  25.     $where .= $wpdb->prepare( ' AND m.meta_key = %s', $date_field );
  26.     $join   = " INNER JOIN $wpdb->postmeta AS m ON m.post_id = p.ID";
  27.       
  28.     $where  = apply_filters( 'getarchives_where', $where, $r );
  29.     $join   = apply_filters( 'getarchives_join' , $join , $r );
  30.   
  31.     $output = '';
  32.     $query = "$select FROM $wpdb->posts AS p $join $where GROUP BY SUBSTRING($field,1,4), SUBSTRING($field,6,2), SUBSTRING($field,9,2) ORDER BY $field DESC $limit";
  33.     $key = md5( $query );
  34.     $cache = wp_cache_get( 'my_get_year_archives' , 'general' );
  35.     if ( !isset( $cache[ $key ] ) ) {
  36.         $arcresults = $wpdb->get_results( $query );
  37.         $cache[ $key ] = $arcresults;
  38.         wp_cache_set( 'my_get_year_archives', $cache, 'general' );
  39.     } else {
  40.         $arcresults = $cache[ $key ];
  41.     }
  42.     if ( $arcresults ) {
  43.         $afterafter = $after;
  44.         foreach ( (array) $arcresults as $arcresult ) {
  45.             $url = add_query_arg( array( 'meta_key' => $date_field ), get_month_link( $arcresult->year, $arcresult->month, $arcresult->day) );
  46.             $text = sprintf( '%d', $arcresult->year ).'/'.sprintf( '%d', $arcresult->month ).'/'.sprintf( '%d', $arcresult->day );
  47.             if ($show_post_count)
  48.                 $after = ' ('.$arcresult->posts.')' . $afterafter;
  49.             $output .= get_archives_link( $url, $text, $format, $before, $after );
  50.         }
  51.     }
  52.           
  53.     if ( $echo )
  54.         echo $output;
  55.     else
  56.         return $output;
  57. }
  58.   
  59. add_action( 'init', 'my_init' );
  60. function my_init() {
  61.     global $wp;
  62.     $wp->add_query_var( 'meta_key' );
  63. }
  64.   
  65. add_action( 'pre_get_posts', 'my_pre_get_posts' );
  66. function my_pre_get_posts( $query ) {
  67.     if ( $query->is_month ) {
  68.         $meta_query = array(
  69.             array(
  70.                 'key'     => $query->get( 'meta_key' ),
  71.                 'value'   => $query->get( 'year'     ).'/'.sprintf('%02d', $query->get( 'monthnum'     ).'/'.sprintf('%02d', $query->get( 'day'     )),
  72.                 'compare' => 'LIKE'
  73.             ),
  74.         );
  75.   
  76.         $query->set( 'meta_query' , $meta_query );
  77.         $query->set( 'year'       , ''          );
  78.         $query->set( 'monthnum'   , ''          );
  79.         $query->set( 'day'   , ''          );
  80.         $query->set( 'date'       , ''          );
  81.         $query->set( 'meta_key'   , ''          );
  82.         $query->set( 'post_type'  , 'カスタム投稿名');
  83.     }
  84. }
Success #stdin #stdout 0s 82880KB
stdin
Standard input is empty
stdout
function my_get_year_archives( $args = '' ) {
    global $wpdb, $wp_locale;
  
    $defaults = array(
        'date_field' => 'date',
        'format' => 'html',
        'echo' => true,
        'limit' => '',
        'before' => '',
        'after' => '', 
        'show_post_count' => true,
    );
  
    $r = wp_parse_args( $args, $defaults );
    extract( $r, EXTR_SKIP );
  
    if ( '' != $limit ) {
        $limit = absint( $limit );
        $limit = ' LIMIT '.$limit;
    }
  
    $field  = 'm.meta_value';
    $select = "SELECT SUBSTRING($field,1,4) AS `year`, SUBSTRING($field,6,2) AS `month`, SUBSTRING($field,9,2) AS `day`, count(p.ID) AS posts";
    $where  = "WHERE p.post_type = 'カスタム投稿名' AND p.post_status = 'publish'";
    $where .= $wpdb->prepare( ' AND m.meta_key = %s', $date_field );
    $join   = " INNER JOIN $wpdb->postmeta AS m ON m.post_id = p.ID";
      
    $where  = apply_filters( 'getarchives_where', $where, $r );
    $join   = apply_filters( 'getarchives_join' , $join , $r );
  
    $output = '';
    $query = "$select FROM $wpdb->posts AS p $join $where GROUP BY SUBSTRING($field,1,4), SUBSTRING($field,6,2), SUBSTRING($field,9,2) ORDER BY $field DESC $limit";
    $key = md5( $query );
    $cache = wp_cache_get( 'my_get_year_archives' , 'general' );
    if ( !isset( $cache[ $key ] ) ) {
        $arcresults = $wpdb->get_results( $query );
        $cache[ $key ] = $arcresults;
        wp_cache_set( 'my_get_year_archives', $cache, 'general' );
    } else {
        $arcresults = $cache[ $key ];
    }
    if ( $arcresults ) {
        $afterafter = $after;
        foreach ( (array) $arcresults as $arcresult ) {
            $url = add_query_arg( array( 'meta_key' => $date_field ), get_month_link( $arcresult->year, $arcresult->month, $arcresult->day) );
            $text = sprintf( '%d', $arcresult->year ).'/'.sprintf( '%d', $arcresult->month ).'/'.sprintf( '%d', $arcresult->day );
            if ($show_post_count)
                $after = ' ('.$arcresult->posts.')' . $afterafter;
            $output .= get_archives_link( $url, $text, $format, $before, $after );
        }
    }
          
    if ( $echo )
        echo $output;
    else
        return $output;
}
  
add_action( 'init', 'my_init' );
function my_init() {
    global $wp;
    $wp->add_query_var( 'meta_key' );
}
  
add_action( 'pre_get_posts', 'my_pre_get_posts' );
function my_pre_get_posts( $query ) {
    if ( $query->is_month ) {
        $meta_query = array(
            array( 
                'key'     => $query->get( 'meta_key' ),
                'value'   => $query->get( 'year'     ).'/'.sprintf('%02d', $query->get( 'monthnum'     ).'/'.sprintf('%02d', $query->get( 'day'     )),
                'compare' => 'LIKE'
            ),
        );
  
        $query->set( 'meta_query' , $meta_query );
        $query->set( 'year'       , ''          );
        $query->set( 'monthnum'   , ''          );
        $query->set( 'day'   , ''          );
        $query->set( 'date'       , ''          );
        $query->set( 'meta_key'   , ''          );
        $query->set( 'post_type'  , 'カスタム投稿名');
    }
}