
###前提・実現したいこと
WordPressでのお話になります。
カスタムフィールドを利用し、投稿日とは別に「イベント日程」を入力し、その日程での月別一覧を表示したいと考えています。
wp_get_archives で出力されるのと同じような形が希望です。
「イベント日程」は、Advanced Custom Fieldsというプラグインを使い、デイトピッカーで日付を入力しています。
(フィールド名)event_date
(値の例)20170101(yymmdd)
~イメージ~
・2017年1月
・2017年2月
・2017年4月
※リンク先は合致する「イベント日程」が入力された記事のみが表示されるアーカイブ
※該当する「イベント日程」がない場合は出力されない(例:2017年3月のイベントがなければ上記の様に一覧にでてこない)
こちらのサイトで紹介されていることが希望に近いと思います。
(この記事とは違いカスタム投稿タイプは使用しません)
http://www.p-nt.com/technicblog/archives/95
###発生している問題・エラーメッセージ
下記のコードで月別一覧のリンクまでは生成されたのですが、
リンク先が404になります。
「・2017年1月」をクリックした先のURLは、
http://example.com?m=201701&meta_key=event_date
となっています。
この404が解消されれば(合致する「イベント日程」の記事が表示されれば)希望通りの動作になります。
###該当のソースコード
// 出力したい場所に記述 <?php my_get_year_archives( array( 'date_field' => 'event_date' ) ); ?>
// functions.phpに記述 function my_get_year_archives( $args = '' ) { global $wpdb, $wp_locale; $defaults = array( 'type' => 'monthly', 'date_field' => '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 ) { // 先頭から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( 'date' , '' ); $query->set( 'meta_key' , '' ); } }
###試したこと
http://wpxtreme.jp/yearly-archive-using-custom-field-in-wordpress
http://www.p-nt.com/technicblog/archives/95
上記2つの記事を参考に、meta_keyを「event_date」に置き換え試しました。
正常に動作させるにはどのようにすればよいでしょうか。
お力添え頂けると幸いです。宜しくお願い致します。
###補足情報(言語/FW/ツール等のバージョンなど)
WordPress 4.8
テーマは Twenty Seventeen です。
(補足)Advanced Custom Field での設定画面のスクリーンショットになります。
一応yymmddで設定していました。
回答1件
あなたの回答
tips
プレビュー