デートピッカーのカスタムフィールド「event_date」で出力したアーカイブページを、こちらの方法を参考にして作成致しました。https://teratail.com/questions/84777
アーカイブを生成する事はクリアできましたが、archive内の並び順が、公開順となってしまい、
orderの指定をしても実行されず困っております。
アーカイブ作成のソースは以下 functions.php
function my_get_year_archives( $args = '' ) { global $wpdb, $wp_locale; $defaults = array( 'type' => 'monthly', 'date_field' => 'event_date', 'format' => 'html', 'echo' => true, 'show_post_count' => false, ); $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,6) AS `yearmonth`, 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,6) 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 ) { $arcresult->year = intval(mb_substr($arcresult->yearmonth, 0, 4)); $arcresult->month = intval(mb_substr($arcresult->yearmonth, 4, 2)); $url = add_query_arg( array( 'meta_key' => $date_field ), get_month_link( $arcresult->year, $arcresult->month ) ); $text = sprintf( '%s', $arcresult->year ).'.'.sprintf( '%02d', $arcresult->month ).''; 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' )), 'compare' => 'LIKE' ), ); $query->set( 'meta_query' , $meta_query ); $query->set( 'year' , '' ); $query->set( 'monthnum' , '' ); $query->set( 'date' , '' ); $query->set( 'meta_key' , '' ); } }
呼び出しのソース (ループ部分)は以下 archive.php
5月のアーカイブページで5月の記事が登録順に並びます。
<?php while ( have_posts()) : the_post(); $date = get_field('event_date', false, false); $date = new DateTime($date); ?> <?php echo $date->format('Y M D'); ?> <?php endwhile; ?>
archive.phpのループにorderを入れた場合、
5月のアーカイブに関わらず、4月の記事も入ってしまいます。
<?php $eventlist = array( 'orderby' => 'meta_value', 'meta_key' => 'event_date', 'order'=> 'ASC' ); $ev_query = new WP_Query($eventlist); while($ev_query->have_posts()) : $ev_query->the_post(); $date = get_field('event_date', false, false); $date = new DateTime($date); ?> <?php echo $date->format('Y M D'); ?> <?php endwhile; ?>
ためした事としてfunctions.phpのアーカイブ呼び出しのarrayの中に
orderを指定しても、実行はされませんでした。
function my_get_year_archives( $args = '' ) { global $wpdb, $wp_locale; $defaults = array( 'type' => 'monthly', 'date_field' => 'event_date', 'format' => 'html', 'echo' => true, 'show_post_count' => false, 'orderby' => 'meta_value', 'meta_key' => 'event_date', 'order'=> 'ASC' ); $r = wp_parse_args( $args, $defaults ); extract( $r, EXTR_SKIP ); if ( '' != $limit ) { $limit = absint( $limit ); $limit = ' LIMIT '.$limit; }
+ で実装したい内容として、
・カテゴリで分かれたアーカイブ (福岡の年月別リスト、東京の年月別リスト)など
・アーカイブページでの、prev nextリンクの追加。
お助け頂ければ幸いです。何卒宜しくお願い致します。

あなたの回答
tips
プレビュー