質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.51%
WordPress

WordPressは、PHPで開発されているオープンソースのブログソフトウェアです。データベース管理システムにはMySQLを用いています。フリーのブログソフトウェアの中では最も人気が高く、PHPとHTMLを使って簡単にテンプレートをカスタマイズすることができます。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

データベース

データベースとは、データの集合体を指します。また、そのデータの集合体の共用を可能にするシステムの意味を含めます

プラグイン

プラグイン(plug-in)は、ソフトウェアアプリケーションの機能拡張の為に開発された、一組のソフトウェアコンポーネントのことを指します。

Q&A

1回答

6074閲覧

カスタムフィールドの日付で月別・カテゴリー毎のアーカイブを作りたい

raidomaru

総合スコア106

WordPress

WordPressは、PHPで開発されているオープンソースのブログソフトウェアです。データベース管理システムにはMySQLを用いています。フリーのブログソフトウェアの中では最も人気が高く、PHPとHTMLを使って簡単にテンプレートをカスタマイズすることができます。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

データベース

データベースとは、データの集合体を指します。また、そのデータの集合体の共用を可能にするシステムの意味を含めます

プラグイン

プラグイン(plug-in)は、ソフトウェアアプリケーションの機能拡張の為に開発された、一組のソフトウェアコンポーネントのことを指します。

0グッド

1クリップ

投稿2017/02/28 09:02

編集2022/01/12 10:55

###前提・実現したいこと
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 = '&nbsp;('.$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 = '&nbsp;('.$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}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2017/02/28 09:28

コピペ元のページに書かれている`my_init()`と`my_pre_get_posts()`を書いていないのはなぜですか?
raidomaru

2017/02/28 09:44

functions.phpには記入してましたが、こちらに記載漏れをしておりましたので修正いたしました。
guest

回答1

0

元ソースから、どのように改変したかを書いていただけると(加えてソースコードへ処理内容についてコメントも入れてもらえると)わかりやすいのですが……。

ぱっと見、以下も修正が必要では?

PHP

1//$query->set( 'date' , '' ); 2$query->set( 'startdate' , '' );

追記
月別にするなら、以下のような修正で。
ただし、カスタムフィールドの値は"2016年01月13日"のような形で、月はゼロ付きの二桁表記である必要があります。
もう少し改良を加えれば、上記の制限を解消して、カテゴリ別などもできるかと思います。

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 // 修正 25 $select = "SELECT SUBSTRING($field,1,8) 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 // 修正 35 $query = "$select FROM $wpdb->posts AS p $join $where GROUP BY SUBSTRING($field,1,8) 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 // 修正 50 $arcresult->year = intval(mb_substr($arcresult->yearmonth, 0, 4)); 51 $arcresult->month = intval(mb_substr($arcresult->yearmonth, 5, 2)); 52 $url = add_query_arg( array( 'meta_key' => $date_field ), get_month_link( $arcresult->year, $arcresult->month ) ); 53 $text = sprintf( '%s', $arcresult->yearmonth ); 54 if ($show_post_count) 55 $after = '&nbsp;('.$arcresult->posts.')' . $afterafter; 56 $output .= get_archives_link( $url, $text, $format, $before, $after ); 57 } 58 } 59 60 if ( $echo ) 61 echo $output; 62 else 63 return $output; 64} 65 66add_action( 'init', 'my_init' ); 67function my_init() { 68 global $wp; 69 $wp->add_query_var( 'meta_key' ); 70} 71 72add_action( 'pre_get_posts', 'my_pre_get_posts' ); 73function my_pre_get_posts( $query ) { 74 75 if ( $query->is_month ) { 76 $meta_query = array( 77 array( 78 'key' => $query->get( 'meta_key' ), 79 // 修正 80 'value' => $query->get( 'year' ).'年'.sprintf("%02d",$query->get( 'monthnum' )).'月', 81 'compare' => 'LIKE' 82 ), 83 ); 84 $query->set( 'meta_query' , $meta_query ); 85 $query->set( 'year' , '' ); 86 // 修正 87 $query->set( 'monthnum' , '' ); 88 $query->set( 'startdate' , '' ); 89 $query->set( 'meta_key' , '' ); 90 } 91}

投稿2017/02/28 10:41

編集2017/03/01 09:18
退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

raidomaru

2017/02/28 11:11

ご指摘ありがとうございます。参考サイトから自分で行った修正と頂いた指摘部分の修正をいたしました。
退会済みユーザー

退会済みユーザー

2017/03/01 09:20

月別、カテゴリ別にするための修正はまだまったく行っていない状況でしたか。 ひとまず月別にする方法だけ、追記しました。
raidomaru

2017/03/02 15:55 編集

ありがとうございます。カスタムフィールドの値が20160113という数字だけの設定でしたので頂いた回答をもとに色々試しながら修正してできました。 カテゴリ別にするのも引き続き取り組んでみます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.51%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問