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

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

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

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

Q&A

1回答

977閲覧

カスタムフィールドの数値でカレンダーを生成

nino_rev

総合スコア6

WordPress

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

0グッド

0クリップ

投稿2018/03/18 03:26

編集2022/01/12 10:55

カスタムフィールドにて日付を指定している場合に、
そちらの日付でカレンダーに出力したいのですが、
現状、投稿日で出力されてしまっています。

イベントカレンダー自体は
カスタム投稿タイプのカレンダーを表示する
上記の記事を元に生成しております。

また、以前の投稿で、こちらの質問を拝見しました。
WordPressのカレンダーをカスタムフィールドの日付と関連付けたい

実現したい仕様に関してはこちらと同様ですが、
今回案件上プラグインの導入が難しいため、
上記のコードでどのような変更・加筆を行えば
カスタムフィールドの日付と関連づけることが可能かを
ご教授いただきたいです。

宜しくお願い致します。

/*追記 180319
ご質問いただきました実際に使用しているコードは以下の通りです。

php

1function get_cpt_calendar($cpt, $initial = true, $echo = true) { 2 global $wpdb, $m, $monthnum, $year, $wp_locale, $posts; 3 4 $cache = array(); 5 $key = md5( $m . $monthnum . $year ); 6 if ( $cache = wp_cache_get( 'get_calendar', 'calendar' ) ) { 7 if ( is_array($cache) && isset( $cache[ $key ] ) ) { 8 if ( $echo ) { 9 echo apply_filters( 'get_calendar', $cache[$key] ); 10 return; 11 } else { 12 return apply_filters( 'get_calendar', $cache[$key] ); 13 } 14 } 15 } 16 17 if ( !is_array($cache) ) 18 $cache = array(); 19 20 // Quick check. If we have no posts at all, abort! 21 if ( !$posts ) { 22 $gotsome = $wpdb->get_var("SELECT 1 as test FROM $wpdb->posts WHERE post_type = '$cpt' AND post_status = 'publish' LIMIT 1"); 23 if ( !$gotsome ) { 24 $cache[ $key ] = ''; 25 wp_cache_set( 'get_calendar', $cache, 'calendar' ); 26 return; 27 } 28 } 29 30 if ( isset($_GET['w']) ) 31 $w = ''.intval($_GET['w']); 32 33 // week_begins = 0 stands for Sunday 34 $week_begins = intval(get_option('start_of_week')); 35 36 // Let's figure out when we are 37 if ( !empty($monthnum) && !empty($year) ) { 38 $thismonth = ''.zeroise(intval($monthnum), 2); 39 $thisyear = ''.intval($year); 40 } elseif ( !empty($w) ) { 41 // We need to get the month from MySQL 42 $thisyear = ''.intval(substr($m, 0, 4)); 43 $d = (($w - 1) * 7) + 6; //it seems MySQL's weeks disagree with PHP's 44 $thismonth = $wpdb->get_var("SELECT DATE_FORMAT((DATE_ADD('{$thisyear}0101', INTERVAL $d DAY) ), '%m')"); 45 } elseif ( !empty($m) ) { 46 $thisyear = ''.intval(substr($m, 0, 4)); 47 if ( strlen($m) < 6 ) 48 $thismonth = '01'; 49 else 50 $thismonth = ''.zeroise(intval(substr($m, 4, 2)), 2); 51 } else { 52 $thisyear = gmdate('Y', current_time('timestamp')); 53 $thismonth = gmdate('m', current_time('timestamp')); 54 } 55 56 $unixmonth = mktime(0, 0 , 0, $thismonth, 1, $thisyear); 57 $last_day = date('t', $unixmonth); 58 59 // Get the next and previous month and year with at least one post 60 $previous = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year 61 FROM $wpdb->posts 62 WHERE post_date < '$thisyear-$thismonth-01' 63 AND post_type = '$cpt' AND post_status = 'publish' 64 ORDER BY post_date DESC 65 LIMIT 1"); 66 $next = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year 67 FROM $wpdb->posts 68 WHERE post_date > '$thisyear-$thismonth-{$last_day} 23:59:59' 69 AND post_type = '$cpt' AND post_status = 'publish' 70 ORDER BY post_date ASC 71 LIMIT 1"); 72 73 /* translators: Calendar caption: 1: month name, 2: 4-digit year */ 74 $calendar_caption = _x('%1$s %2$s', 'calendar caption'); 75 $calendar_output = '<table id="wp-calendar"> 76 <caption>' . sprintf($calendar_caption, $wp_locale->get_month($thismonth), date('Y', $unixmonth)) . '</caption> 77 <thead> 78 <tr>'; 79 80 $myweek = array(); 81 82 for ( $wdcount=0; $wdcount<=6; $wdcount++ ) { 83 $myweek[] = $wp_locale->get_weekday(($wdcount+$week_begins)%7); 84 } 85 86 foreach ( $myweek as $wd ) { 87 $day_name = (true == $initial) ? $wp_locale->get_weekday_initial($wd) : $wp_locale->get_weekday_abbrev($wd); 88 $wd = esc_attr($wd); 89 $calendar_output .= "\n\t\t<th scope=\"col\" title=\"$wd\">$day_name</th>"; 90 } 91 92 $calendar_output .= ' 93 </tr> 94 </thead> 95 96 <tfoot> 97 <tr>'; 98 99 if ( $previous ) { 100 $calendar_output .= "\n\t\t".'<td colspan="3" id="prev"><a href="' . get_month_link($previous->year, $previous->month) . '?post_type='.$cpt.'" title="' . esc_attr( sprintf(__('View posts for %1$s %2$s'), $wp_locale->get_month($previous->month), date('Y', mktime(0, 0 , 0, $previous->month, 1, $previous->year)))) . '">&laquo; ' . $wp_locale->get_month_abbrev($wp_locale->get_month($previous->month)) . '</a></td>'; 101 } else { 102 $calendar_output .= "\n\t\t".'<td colspan="3" id="prev" class="pad">&nbsp;</td>'; 103 } 104 105 $calendar_output .= "\n\t\t".'<td class="pad">&nbsp;</td>'; 106 107 if ( $next ) { 108 $calendar_output .= "\n\t\t".'<td colspan="3" id="next"><a href="' . get_month_link($next->year, $next->month) . '?post_type='.$cpt.'" title="' . esc_attr( sprintf(__('View posts for %1$s %2$s'), $wp_locale->get_month($next->month), date('Y', mktime(0, 0 , 0, $next->month, 1, $next->year))) ) . '">' . $wp_locale->get_month_abbrev($wp_locale->get_month($next->month)) . ' &raquo;</a></td>'; 109 } else { 110 $calendar_output .= "\n\t\t".'<td colspan="3" id="next" class="pad">&nbsp;</td>'; 111 } 112 113 $calendar_output .= ' 114 </tr> 115 </tfoot> 116 117 <tbody> 118 <tr>'; 119 120 // Get days with posts 121 $dayswithposts = $wpdb->get_results("SELECT DISTINCT DAYOFMONTH(post_date) 122 FROM $wpdb->posts WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00' 123 AND post_type = '$cpt' AND post_status = 'publish' 124 AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59'", ARRAY_N); 125 if ( $dayswithposts ) { 126 foreach ( (array) $dayswithposts as $daywith ) { 127 $daywithpost[] = $daywith[0]; 128 } 129 } else { 130 $daywithpost = array(); 131 } 132 133 if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'camino') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'safari') !== false) 134 $ak_title_separator = "\n"; 135 else 136 $ak_title_separator = ', '; 137 138 $ak_titles_for_day = array(); 139 $ak_post_titles = $wpdb->get_results("SELECT ID, post_title, DAYOFMONTH(post_date) as dom " 140 ."FROM $wpdb->posts " 141 ."WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00' " 142 ."AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59' " 143 ."AND post_type = '$cpt' AND post_status = 'publish'" 144 ); 145 if ( $ak_post_titles ) { 146 foreach ( (array) $ak_post_titles as $ak_post_title ) { 147 148 /** This filter is documented in wp-includes/post-template.php */ 149 $post_title = esc_attr( apply_filters( 'the_title', $ak_post_title->post_title, $ak_post_title->ID ) ); 150 151 if ( empty($ak_titles_for_day['day_'.$ak_post_title->dom]) ) 152 $ak_titles_for_day['day_'.$ak_post_title->dom] = ''; 153 if ( empty($ak_titles_for_day["$ak_post_title->dom"]) ) // first one 154 $ak_titles_for_day["$ak_post_title->dom"] = $post_title; 155 else 156 $ak_titles_for_day["$ak_post_title->dom"] .= $ak_title_separator . $post_title; 157 } 158 } 159 160 // See how much we should pad in the beginning 161 $pad = calendar_week_mod(date('w', $unixmonth)-$week_begins); 162 if ( 0 != $pad ) 163 $calendar_output .= "\n\t\t".'<td colspan="'. esc_attr($pad) .'" class="pad">&nbsp;</td>'; 164 165 $daysinmonth = intval(date('t', $unixmonth)); 166 for ( $day = 1; $day <= $daysinmonth; ++$day ) { 167 if ( isset($newrow) && $newrow ) 168 $calendar_output .= "\n\t</tr>\n\t<tr>\n\t\t"; 169 $newrow = false; 170 171 if ( $day == gmdate('j', current_time('timestamp')) && $thismonth == gmdate('m', current_time('timestamp')) && $thisyear == gmdate('Y', current_time('timestamp')) ) 172 $calendar_output .= '<td id="today">'; 173 else 174 $calendar_output .= '<td>'; 175 176 if ( in_array($day, $daywithpost) ) // any posts today? 177 $calendar_output .= '<a href="' . get_day_link( $thisyear, $thismonth, $day ) . '?post_type='.$cpt.'" title="' . esc_attr( $ak_titles_for_day[ $day ] ) . "\">$day</a>"; 178 else 179 $calendar_output .= $day; 180 $calendar_output .= '</td>'; 181 182 if ( 6 == calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins) ) 183 $newrow = true; 184 } 185 186 $pad = 7 - calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins); 187 if ( $pad != 0 && $pad != 7 ) 188 $calendar_output .= "\n\t\t".'<td class="pad" colspan="'. esc_attr($pad) .'">&nbsp;</td>'; 189 190 $calendar_output .= "\n\t</tr>\n\t</tbody>\n\t</table>"; 191 192 $cache[ $key ] = $calendar_output; 193 wp_cache_set( 'get_calendar', $cache, 'calendar' ); 194 195 if ( $echo ) 196 echo apply_filters( 'get_calendar', $calendar_output ); 197 else 198 return apply_filters( 'get_calendar', $calendar_output ); 199 200}

上記をfunctions.phpに記述し、
カレンダーを出力したい箇所に

php

1<?php get_cpt_calendar('投稿タイプ名'); ?>

上記を記述し呼び出したところ、
記事の投稿日が反映されたカレンダーが表示されております。
こちらをカスタムフィールドで指定した日付と関連づけたいというのは今回の希望です。

お力添えいただければ幸いです。
宜しくお願い致します。

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

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

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

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

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

退会済みユーザー

退会済みユーザー

2018/03/18 22:44

質問文に参考URLをいくつか掲載していますが、実際に使用しているコードとそれによる出力結果を質問文に記載していただけますか。
nino_rev

2018/03/19 00:36

追記させていただきました!
退会済みユーザー

退会済みユーザー

2018/03/19 07:46

追記ありがとうございます。こちらのコードは、参考にしているteratailの質問の回答を反映したものという理解でよろしいでしょうか。
guest

回答1

0

SQLを自分で書くよりもdate_queryとかmeta_queryとか駆使して月を絞り込んで、後はカレンダーとして表示されるように組めば出来ると思います。

【WordPressのdate_queryパラメータを使って、複雑な期間指定の投稿を取得する | Tips Note by TAM】
https://www.tam-tam.co.jp/tipsnote/cms/post6697.html

【WordPressで日付の範囲指定をして記事を表示する方法 | それからデザイン スタッフブログ】
https://sole-color-blog.com/blog/282/

【【pre_get_posts】カスタムフィールドに入力した日付で今日以降の記事を表示 - Brushape】
http://brushape.com/wordpress/【pre_get_posts】カスタムフィールドni日付/

投稿2018/03/25 07:47

kei344

総合スコア69364

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問