目的
Advanced Custom Fieldsのデイトピッカーを使用して月別一覧を表示した際に末尾につく
「?meta_key=event_date」
を削除したい
.
.
.
前提
CPTUI使用(post-type : schedule)
ACF使用 (meta-key : event_date)
以下のようなURLになります。
http://○○○/schedule//2022/06/?meta_key=event_date
.
.
.
現状
以下の記述を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'; $firstday_month = date('Ym01'); $select = "SELECT SUBSTRING($field,1,6) AS `yearmonth`, count(p.ID) AS posts"; $where = "WHERE p.post_type = 'schedule' AND p.post_status = 'publish'"; $where .= $wpdb->prepare( ' AND m.meta_value >= %s', $firstday_month ); $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 ASC $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 ) { // 先頭から4つが年、5番目から2つが月 $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( 'event_date' , '' ); $query->set( 'meta_key' , '' ); $query->set( 'order', 'ASC' ); $query->set( 'post_type' , 'schedule'); } }
.
.
.
課題
以下のURLの末尾についてしまう
「?meta_key=event_date」
を削除させたい
http://○○○/schedule//2022/06/?meta_key=event_date
.
.
.
.
いろいろと勉強不足で不十分な点もあるかと思いますが、どなたかご教授いただければ幸いです。
よろしくお願いいたします。
あなたの回答
tips
プレビュー