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 = 'post' 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. $where = apply_filters( 'getarchives_where', $where, $r );
  28. $join = apply_filters( 'getarchives_join' , $join , $r );
  29.  
  30. $output = '';
  31. $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";
  32. $key = md5( $query );
  33. $cache = wp_cache_get( 'my_get_year_archives' , 'general' );
  34. if ( !isset( $cache[ $key ] ) ) {
  35. $arcresults = $wpdb->get_results( $query );
  36. $cache[ $key ] = $arcresults;
  37. wp_cache_set( 'my_get_year_archives', $cache, 'general' );
  38. } else {
  39. $arcresults = $cache[ $key ];
  40. }
  41. if ( $arcresults ) {
  42. $afterafter = $after;
  43. foreach ( (array) $arcresults as $arcresult ) {
  44. $url = add_query_arg( array( 'meta_key' => $date_field ), get_day_link( $arcresult->year, $arcresult->month, $arcresult->day) );
  45. $text = sprintf( '%d', $arcresult->year ).'/'.sprintf( '%02d', $arcresult->month ).'/'.sprintf( '%02d', $arcresult->day );
  46. if ($show_post_count)
  47. $after = ' ('.$arcresult->posts.')' . $afterafter;
  48. $output .= get_archives_link( $url, $text, $format, $before, $after );
  49. }
  50. }
  51.  
  52. if ( $echo )
  53. echo $output;
  54. else
  55. return $output;
  56. }
  57.  
  58. add_action( 'init', 'my_init' );
  59. function my_init() {
  60. global $wp;
  61. $wp->add_query_var( 'meta_key' );
  62. }
  63.  
  64. add_action( 'pre_get_posts', 'my_pre_get_posts' );
  65. function my_pre_get_posts( $query ) {
  66. if ( $query->is_day ) {
  67. $meta_query = array(
  68. 'key' => $query->get( 'meta_key' ),
  69. 'value' => $query->get( 'year' ).'/'.sprintf('%02d', $query->get( 'monthnum' )).'/'.sprintf('%02d', $query->get( 'day' )),
  70. 'compare' => 'LIKE'
  71. ),
  72. );
  73. $query->set( 'meta_query' , $meta_query );
  74. $query->set( 'year' , '' );
  75. $query->set( 'monthnum' , '' );
  76. $query->set( 'day' , '' );
  77. $query->set( 'date' , '' );
  78. $query->set( 'meta_key' , '' );
  79. $query->set( 'post_type' , 'post');
  80. }
  81. }
Success #stdin #stdout 0.02s 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 = 'post' 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_day_link( $arcresult->year, $arcresult->month, $arcresult->day) );
            $text = sprintf( '%d', $arcresult->year ).'/'.sprintf( '%02d', $arcresult->month ).'/'.sprintf( '%02d', $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_day ) {
        $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'  , 'post');
    }
}