
###前提・実現したいこと
Advanced Custom Fieldsを使用してイベントのカテゴリーの投稿に開始日と終了日を設定しております。
https://ja.wordpress.org/plugins/advanced-custom-fields/
通常の投稿日によるアーカイブではなく、開始日の日付でアーカイブを作りたいと考えております。また、プルダウンメニューで月を選ぶ形にしたいです。
開始日のmeta_keyはstartdateです。
また、イベントの種類によりカテゴリーを分けております。
カテゴリー構造
イベントA
-イベントAの子カテゴリーが3つ
イベントB
-イベントBの子カテゴリーが7つ
###発生している問題
http://wpxtreme.jp/yearly-archive-using-custom-field-in-wordpress
こちらを参考にしてプルダウン内にstart_dateでの年別アーカイブへのリンクを作ることはできたのですが、start_dateを持っている全ての投稿を年別で拾いあげてしまいます。
自身で行った変更点
【category-A.php】
・my_get_year_archives引数のmydateがカスタムフィールドのキーと考えstartdateに変更
【functions.php】
・<li>タグではなく<option>タグによる出力にしたかったので$default内のformatをhtmlからoptionに変更
・$default内date_fieldをdateからstartdateに変更
・件数表示させるために$default内show_post_countをfalseからtrueに変更
これを月別、カテゴリー別にしたいです。
テンプレートとして下記があります。
category-A.php
category-Aの子カテゴリーそれぞれ.php
category-B.php
category-Bの子カテゴリーそれぞれ.php
ご教示頂けますと幸いです。
###category-A.phpの該当部分
php
1<label for="archive">Archive</label> 2 <select id="archive" onchange="location.href=value"> 3 <? 4 //php my_get_year_archives(array('date_field'=>'mydate',)); 5 php my_get_year_archives(array('date_field'=>'startdate',)); 6 ?> 7</select>
###functions.php
php
1function my_get_year_archives( $args = '' ) { 2 global $wpdb, $wp_locale; 3 4 $defaults = array( 5 'type' => 'monthly', 6//カスタムフィールドのキー dateからstartdateに変更 7 'date_field' => 'startdate', 8//セレクトボックス内のoptionにするためhtmlから変更 9 'format' => 'option', 10 'echo' => true, 11 'limit' => '', 12 'before' => '', 13 'after' => '', 14//件数表示をするためfalseから変更 15 'show_post_count' => true, 16 ); 17 18 $r = wp_parse_args( $args, $defaults ); 19 extract( $r, EXTR_SKIP ); 20 21 if ( '' != $limit ) { 22 $limit = absint( $limit ); 23 $limit = ' LIMIT '.$limit; 24 } 25 26 $field = 'm.meta_value'; 27 $select = "SELECT SUBSTRING($field,1,4) AS `year`, count(p.ID) AS posts"; 28 $where = "WHERE p.post_type = 'post' AND p.post_status = 'publish'"; 29 $where .= $wpdb->prepare( ' AND m.meta_key = %s', $date_field ); 30 $join = " INNER JOIN $wpdb->postmeta AS m ON m.post_id = p.ID"; 31 32 $where = apply_filters( 'getarchives_where', $where, $r ); 33 $join = apply_filters( 'getarchives_join' , $join , $r ); 34 35 $output = ''; 36 $query = "$select FROM $wpdb->posts AS p $join $where GROUP BY SUBSTRING($field,1,4) ORDER BY $field DESC $limit"; 37 $key = md5( $query ); 38 $cache = wp_cache_get( 'my_get_year_archives' , 'general' ); 39 if ( !isset( $cache[ $key ] ) ) { 40 $arcresults = $wpdb->get_results( $query ); 41 $cache[ $key ] = $arcresults; 42 wp_cache_set( 'my_get_year_archives', $cache, 'general' ); 43 } else { 44 $arcresults = $cache[ $key ]; 45 } 46 47 if ( $arcresults ) { 48 $afterafter = $after; 49 foreach ( (array) $arcresults as $arcresult ) { 50 $url = add_query_arg( array( 'meta_key' => $date_field ), get_year_link( $arcresult->year ) ); 51 $text = sprintf( '%d', $arcresult->year ); 52 if ($show_post_count) 53 $after = ' ('.$arcresult->posts.')' . $afterafter; 54 $output .= get_archives_link( $url, $text, $format, $before, $after ); 55 } 56 } 57 58 if ( $echo ) 59 echo $output; 60 else 61 return $output; 62} 63 64add_action( 'init', 'my_init' ); 65function my_init() { 66 global $wp; 67 $wp->add_query_var( 'meta_key' ); 68} 69 70add_action( 'pre_get_posts', 'my_pre_get_posts' ); 71function my_pre_get_posts( $query ) { 72 if ( $query->is_year ) { 73 $meta_query = array( 74 array( 75 'key' => $query->get( 'meta_key' ), 76 'value' => $query->get( 'year' ), 77 'compare' => 'LIKE' 78 ), 79 ); 80 81 $query->set( 'meta_query' , $meta_query ); 82 $query->set( 'year' , '' ); 83//ご指摘頂きdateからstartdateに変更 84 $query->set( 'startdate' , '' ); 85 $query->set( 'meta_key' , '' ); 86 } 87}
###修正したfuncstions.php
php
1function my_get_year_archives( $args = '' ) { 2 global $wpdb, $wp_locale; 3 4 $defaults = array( 5 'type' => 'monthly', 6 'date_field' => 'startdate', 7 'format' => 'option', 8 'echo' => true, 9 'limit' => '', 10 'before' => '', 11 'after' => '', 12 'show_post_count' => true, 13 ); 14 15 $r = wp_parse_args( $args, $defaults ); 16 extract( $r, EXTR_SKIP ); 17 18 if ( '' != $limit ) { 19 $limit = absint( $limit ); 20 $limit = ' LIMIT '.$limit; 21 } 22 23 $field = 'm.meta_value'; 24 // 修正 年と月を取ってyearmonthとする 25 $select = "SELECT SUBSTRING($field,1,6) AS `yearmonth`, count(p.ID) AS posts"; 26 $where = "WHERE p.post_type = 'post' AND p.post_status = 'publish'"; 27 $where .= $wpdb->prepare( ' AND m.meta_key = %s', $date_field ); 28 $join = " INNER JOIN $wpdb->postmeta AS m ON m.post_id = p.ID"; 29 30 $where = apply_filters( 'getarchives_where', $where, $r ); 31 $join = apply_filters( 'getarchives_join' , $join , $r ); 32 33 $output = ''; 34 // 修正 年と月を取ってyearmonthとする 35 $query = "$select FROM $wpdb->posts AS p $join $where GROUP BY SUBSTRING($field,1,6) ORDER BY $field DESC $limit"; 36 $key = md5( $query ); 37 $cache = wp_cache_get( 'my_get_year_archives' , 'general' ); 38 if ( !isset( $cache[ $key ] ) ) { 39 $arcresults = $wpdb->get_results( $query ); 40 $cache[ $key ] = $arcresults; 41 wp_cache_set( 'my_get_year_archives', $cache, 'general' ); 42 } else { 43 $arcresults = $cache[ $key ]; 44 } 45 46 if ( $arcresults ) { 47 $afterafter = $after; 48 foreach ( (array) $arcresults as $arcresult ) { 49 // 修正 先頭から4つが年、5番目から2つが月 50 $arcresult->year = intval(mb_substr($arcresult->yearmonth, 0, 4)); 51 $arcresult->month = intval(mb_substr($arcresult->yearmonth, 4, 2)); 52 $url = add_query_arg( array( 'meta_key' => $date_field ), get_month_link( $arcresult->year, $arcresult->month ) ); 53 // 修正 ここがプルダウン部分のテキストになる 54 $text = sprintf( '%s', $arcresult->year ).'年'.sprintf( '%02d', $arcresult->month ).'月'; 55 if ($show_post_count) 56 $after = ' ('.$arcresult->posts.')' . $afterafter; 57 $output .= get_archives_link( $url, $text, $format, $before, $after ); 58 } 59 } 60 61 if ( $echo ) 62 echo $output; 63 else 64 return $output; 65} 66 67add_action( 'init', 'my_init' ); 68function my_init() { 69 global $wp; 70 $wp->add_query_var( 'meta_key' ); 71} 72 73add_action( 'pre_get_posts', 'my_pre_get_posts' ); 74function my_pre_get_posts( $query ) { 75 76 if ( $query->is_month ) { 77 $meta_query = array( 78 array( 79 'key' => $query->get( 'meta_key' ), 80 // 修正 カスタムフィールドの形式に合わせる 81 'value' => $query->get( 'year' ).sprintf("%02d",$query->get( 'monthnum' )), 82 'compare' => 'LIKE' 83 ), 84 ); 85 $query->set( 'meta_query' , $meta_query ); 86 $query->set( 'year' , '' ); 87 // 修正 88 $query->set( 'monthnum' , '' ); 89 $query->set( 'startdate' , '' ); 90 $query->set( 'meta_key' , '' ); 91 } 92}