function my_get_year_archives( $args = '' ) {
global $wpdb , $wp_locale ;
'date_field' => 'date' ,
'format' => 'html' ,
'echo' => true ,
'limit' => '' ,
'before' => '' ,
'after' => '' ,
'show_post_count' => true ,
) ;
$r = wp_parse_args( $args , $defaults ) ;
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 " ;
$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 ) {
'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' ) ;
}
}
ZnVuY3Rpb24gbXlfZ2V0X3llYXJfYXJjaGl2ZXMoICRhcmdzID0gJycgKSB7CglnbG9iYWwgJHdwZGIsICR3cF9sb2NhbGU7CgoJJGRlZmF1bHRzID0gYXJyYXkoCiAgICAgICAgJ2RhdGVfZmllbGQnID0+ICdkYXRlJywKICAgICAgICAnZm9ybWF0JyA9PiAnaHRtbCcsCiAgICAgICAgJ2VjaG8nID0+IHRydWUsCiAgICAgICAgJ2xpbWl0JyA9PiAnJywKICAgICAgICAnYmVmb3JlJyA9PiAnJywKICAgICAgICAnYWZ0ZXInID0+ICcnLCAKICAgICAgICAnc2hvd19wb3N0X2NvdW50JyA9PiB0cnVlLAogICAgKTsKICAKICAgICRyID0gd3BfcGFyc2VfYXJncyggJGFyZ3MsICRkZWZhdWx0cyApOwogICAgZXh0cmFjdCggJHIsIEVYVFJfU0tJUCApOwogIAogICAgaWYgKCAnJyAhPSAkbGltaXQgKSB7CiAgICAgICAgJGxpbWl0ID0gYWJzaW50KCAkbGltaXQgKTsKICAgICAgICAkbGltaXQgPSAnIExJTUlUICcuJGxpbWl0OwogICAgfQogIAogICAgJGZpZWxkICA9ICdtLm1ldGFfdmFsdWUnOwogICAgJHNlbGVjdCA9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z2V0X3Jlc3VsdHMoICRxdWVyeSApOwogICAgICAgICRjYWNoZVsgJGtleSBdID0gJGFyY3Jlc3VsdHM7CiAgICAgICAgd3BfY2FjaGVfc2V0KCAnbXlfZ2V0X3llYXJfYXJjaGl2ZXMnLCAkY2FjaGUsICdnZW5lcmFsJyApOwogICAgfSBlbHNlIHsKICAgICAgICAkYXJjcmVzdWx0cyA9ICRjYWNoZVsgJGtleSBdOwogICAgfQogICAgaWYgKCAkYXJjcmVzdWx0cyApIHsKICAgICAgICAkYWZ0ZXJhZnRlciA9ICRhZnRlcjsKICAgICAgICBmb3JlYWNoICggKGFycmF5KSAkYXJjcmVzdWx0cyBhcyAkYXJjcmVzdWx0ICkgewogICAgICAgICAgICAkdXJsID0gYWRkX3F1ZXJ5X2FyZyggYXJyYXkoICdtZXRhX2tleScgPT4gJGRhdGVfZmllbGQgKSwgZ2V0X2RheV9saW5rKCAkYXJjcmVzdWx0LT55ZWFyLCAkYXJjcmVzdWx0LT5tb250aCwgJGFyY3Jlc3VsdC0+ZGF5KSApOwogICAgICAgICAgICAkdGV4dCA9IHNwcmludGYoICclZCcsICRhcmNyZXN1bHQtPnllYXIgKS4nLycuc3ByaW50ZiggJyUwMmQnLCAkYXJjcmVzdWx0LT5tb250aCApLicvJy5zcHJpbnRmKCAnJTAyZCcsICRhcmNyZXN1bHQtPmRheSApOwogICAgICAgICAgICBpZiAoJHNob3dfcG9zdF9jb3VudCkKICAgICAgICAgICAgICAgICRhZnRlciA9ICcmbmJzcDsoJy4kYXJjcmVzdWx0LT5wb3N0cy4nKScgLiAkYWZ0ZXJhZnRlcjsKICAgICAgICAgICAgJG91dHB1dCAuPSBnZXRfYXJjaGl2ZXNfbGluayggJHVybCwgJHRleHQsICRmb3JtYXQsICRiZWZvcmUsICRhZnRlciApOwogICAgICAgIH0KICAgIH0KICAgICAgICAgIAogICAgaWYgKCAkZWNobyApCiAgICAgICAgZWNobyAkb3V0cHV0OwogICAgZWxzZQogICAgICAgIHJldHVybiAkb3V0cHV0Owp9CiAgCmFkZF9hY3Rpb24oICdpbml0JywgJ215X2luaXQnICk7CmZ1bmN0aW9uIG15X2luaXQoKSB7CiAgICBnbG9iYWwgJHdwOwogICAgJHdwLT5hZGRfcXVlcnlfdmFyKCAnbWV0YV9rZXknICk7Cn0KICAKYWRkX2FjdGlvbiggJ3ByZV9nZXRfcG9zdHMnLCAnbXlfcHJlX2dldF9wb3N0cycgKTsKZnVuY3Rpb24gbXlfcHJlX2dldF9wb3N0cyggJHF1ZXJ5ICkgewogICAgaWYgKCAkcXVlcnktPmlzX2RheSApIHsKICAgICAgICAkbWV0YV9xdWVyeSA9IGFycmF5KAogICAgICAgICAgICBhcnJheSggCiAgICAgICAgICAgICAgICAna2V5JyAgICAgPT4gJHF1ZXJ5LT5nZXQoICdtZXRhX2tleScgKSwKICAgICAgICAgICAgICAgICd2YWx1ZScgICA9PiAkcXVlcnktPmdldCggJ3llYXInICAgICApLicvJy5zcHJpbnRmKCclMDJkJywgJHF1ZXJ5LT5nZXQoICdtb250aG51bScgICAgICkpLicvJy5zcHJpbnRmKCclMDJkJywgJHF1ZXJ5LT5nZXQoICdkYXknICAgICApKSwKICAgICAgICAgICAgICAgICdjb21wYXJlJyA9PiAnTElLRScKICAgICAgICAgICAgKSwKICAgICAgICApOwogICAgICAgICRxdWVyeS0+c2V0KCAnbWV0YV9xdWVyeScgLCAkbWV0YV9xdWVyeSApOwogICAgICAgICRxdWVyeS0+c2V0KCAneWVhcicgICAgICAgLCAnJyAgICAgICAgICApOwogICAgICAgICRxdWVyeS0+c2V0KCAnbW9udGhudW0nICAgLCAnJyAgICAgICAgICApOwogICAgICAgICRxdWVyeS0+c2V0KCAnZGF5JyAgICwgJycgICAgICAgICAgKTsKICAgICAgICAkcXVlcnktPnNldCggJ2RhdGUnICAgICAgICwgJycgICAgICAgICAgKTsKICAgICAgICAkcXVlcnktPnNldCggJ21ldGFfa2V5JyAgICwgJycgICAgICAgICAgKTsKICAgICAgICAkcXVlcnktPnNldCggJ3Bvc3RfdHlwZScgICwgJ3Bvc3QnKTsKICAgIH0KfQ==
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');
}
}