前提・実現したいこと
カスタム投稿イベント(event)にACFで開催日を登録するフィールド(open_day)を設けています。
デイトピッカーで返り値のフォーマットは20211014(Ymd)です。
サイドバーに開催日に登録されている日付での月別一覧のアーカイブをセレクトボックスで表示させたいです。
発生している問題・エラーメッセージ
こちらの記事参考に作成してみましたがあり得ない値が取得されてきています。
https://teratail.com/questions/306523
該当のソースコード
finctions.php
//カスタムフィールドの値で月別アーカイブ function my_get_month_archives( $args = '' ) { global $wpdb, $wp_locale; $defaults = array( 'date_field' => 'open_day', //カスタムフィールドのフィールド名を記述 '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,5,2) AS `month`, count(p.ID) AS posts"; $where = "WHERE p.post_type = 'event' 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,5,2) ORDER BY $field DESC $limit"; $key = md5( $query ); $cache = wp_cache_get( 'my_get_month_archives' , 'general' ); if ( !isset( $cache[ $key ] ) ) { $arcresults = $wpdb->get_results( $query ); $cache[ $key ] = $arcresults; wp_cache_set( 'my_get_month_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_month_link( $arcresult->year, $arcresult->month) ); $text = sprintf( '%d', $arcresult->year ).'年'.sprintf( '%d', $arcresult->month ).'月'; $output .= '<option value="'.$url.'">'.$text.'</option>'; } } if ( $echo ) echo $output; else return $output; }
sidebar.php
<select name="archive-dropdown" onChange='document.location.href=this.options[this.selectedIndex].value;'> <option value="">月別アーカイブ</option> <?php my_get_month_archives( array( 'date_field' => 'open_day', //カスタムフィールドのフィールド名を記述 ) ); ?> </select>
メッセージ
どのように修正すれば正しい値が表示されるようになるでしょうか。
できれば過去のイベント(昨日以前)とこれからのイベント(今日以降)でわけてそれぞれセレクトボックスを設定できるようにしたいです。
ご教示いただけると助かります。

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2021/10/14 04:06
退会済みユーザー
2021/10/14 08:24
退会済みユーザー
2021/10/17 10:33