目的
Advanced Custom Fieldsのデイトピッカーを使用して今日よりも未来の月別一覧を表示したい
.
.
.
前提
CPTUI使用(post-type : schedule)
ACF使用 (meta-key : event_date)
以下のようなURLになります。
http://○○○/schedule//2022/05/?meta_key=event_date
.
.
.
現状
いろいろな記事を参考にしながらfunctions.phpに以下のコードを記載し
投稿した全ての月の一覧ページを取得することはできました
functions.php
1function my_get_year_archives( $args = '' ) { 2 global $wpdb, $wp_locale; 3 $defaults = array( 4 'type' => 'monthly', 5 'date_field' => 'event_date', 6 'format' => 'html', 7 'echo' => true, 8 'show_post_count' => false, 9 ); 10 11 $r = wp_parse_args( $args, $defaults ); 12 extract( $r, EXTR_SKIP ); 13 14 if ( '' != $limit ) { 15 $limit = absint( $limit ); 16 $limit = ' LIMIT '.$limit; 17 } 18 19 20 21 $field = 'm.meta_value'; 22 $select = "SELECT SUBSTRING($field,1,6) AS `yearmonth`, count(p.ID) AS posts"; 23 $where = "WHERE p.post_type = 'schedule' AND p.post_status = 'publish'"; 24 $where .= $wpdb->prepare( ' AND m.meta_key = %s', $date_field ); 25 $join = "INNER JOIN $wpdb->postmeta AS m ON m.post_id = p.ID"; 26 27 $where = apply_filters( 'getarchives_where', $where, $r ); 28 $join = apply_filters( 'getarchives_join' , $join , $r ); 29 30 31 32 $output = ''; 33 $query = "$select FROM $wpdb->posts AS p $join $where GROUP BY SUBSTRING($field,1,6) ORDER BY $field ASC $limit"; 34 $key = md5( $query ); 35 $cache = wp_cache_get( 'my_get_year_archives' , 'general' ); 36 if ( !isset( $cache[ $key ] ) ) { 37 $arcresults = $wpdb->get_results( $query ); 38 $cache[ $key ] = $arcresults; 39 wp_cache_set( 'my_get_year_archives', $cache, 'general' ); 40 } else { 41 $arcresults = $cache[ $key ]; 42 } 43 44 45 46 if ( $arcresults ) { 47 $afterafter = $after; 48 49 foreach ( (array) $arcresults as $arcresult ) { 50 // 先頭から4つが年、5番目から2つが月 51 $arcresult->year = intval(mb_substr($arcresult->yearmonth, 0, 4)); 52 $arcresult->month = intval(mb_substr($arcresult->yearmonth, 4, 2)); 53 54 $url = add_query_arg( array( 'meta_key' => $date_field ), get_month_link( $arcresult->year, $arcresult->month ) ); 55 $text = sprintf( '%s', $arcresult->year ).'年'.sprintf( '%02d', $arcresult->month ).'月'; 56 if ($show_post_count) 57 $after = ' ('.$arcresult->posts.')' . $afterafter; 58 $output .= get_archives_link( $url, $text, $format, $before, $after ); 59 } 60 } 61 62 63 if ( $echo ) 64 echo $output; 65 else 66 return $output; 67} 68 69add_action( 'init', 'my_init' ); 70function my_init() { 71 global $wp; 72 $wp->add_query_var( 'meta_key' ); 73} 74 75add_action( 'pre_get_posts', 'my_pre_get_posts' ); 76 function my_pre_get_posts( $query ) { 77 if ( $query->is_month ) { 78 $meta_query = array( 79 array( 80 'key' => $query->get( 'meta_key' ), 81 'value' => $query->get('year') . sprintf('%02d', $query->get('monthnum')), 82 'compare' => 'LIKE', 83 84 ), 85 ); 86 $query->set( 'meta_query' , $meta_query ); 87 $query->set( 'year' , '' ); 88 $query->set( 'monthnum' , '' ); 89 $query->set( 'event_date' , '' ); 90 $query->set( 'meta_key' , '' ); 91 $query->set( 'order', 'ASC' ); 92 $query->set( 'post_type' , 'schedule'); 93 } 94 95}
.
.
.
課題
過去の記事は添付画像の「過去」に表示されるようにしているので
現在より過去の月を月別一覧を取得させないようにしたいです。
.
.
.
試行済み
1,sidebar.phpに以下のコードを追加してみたりしたのですが反応がありませんでした
before
sidebar.php
1 <?php 2 my_get_year_archives(array( 3 'date_field' => 'event_date', 4 5 )); 6 ?>
after
sidebar.php
1 <?php 2 $today = date('Ymd'); 3 my_get_year_archives(array( 4 'date_field' => 'event_date', 5 'meta_value' => $today, 6 'meta_compare' => '>=', 7 'orderby' => 'meta_value', 8 )); 9 ?>
.
.
.
.
いろいろと勉強不足で不十分な点もあるかと思いますが、どなたかご教授いただければ幸いです。
よろしくお願いいたします。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/06/20 08:52