前提・実現したいこと
WordPressに詳しい方助けてください。
やりたいことを調べていたら、https://teratail.com/questions/120135 のページにたどり着きました。
やりたいことはまさにこの質問と同じことなのですがこちらの回答にあったTransientsの使い方が分からず同じサイトで質問しようとこちらのサイトへ登録しました。
発生している問題・エラーメッセージ
エラーメッセージなどは表示されていません
該当のソースコード
今はこちらを書いて表示しています。
php
1function test_year(){ 2 global $wpdb; 3 $firstdate = $wpdb->get_var("SELECT post_date from $wpdb->posts where post_status = 'publish' and post_type = 'post' ORDER BY post_date ASC LIMIT 1"); 4 $yyyy = intval(substr($firstdate, 0, 4)); 5 return $yyyy; 6} 7echo test_year(); 8
試したこと
https://wpdocs.osdn.jp/Transients_API を見たのですが、イマイチよくわかりません。
php
1if ( false === ( $special_query_results = get_transient( 'special_query_results' ) ) ) { 2 function test_year(){ 3 global $wpdb; 4 $firstdate = $wpdb->get_var("SELECT post_date from $wpdb->posts where post_status = 'publish' and post_type = 'post' ORDER BY post_date ASC LIMIT 1"); 5 $yyyy = intval(substr($firstdate, 0, 4)); 6 return $yyyy; 7 } 8 set_transient( 'special_query_results', $special_query_results, 12 * HOUR_IN_SECONDS ); 9} 10echo test_year();
としてみたのですが、何も表示されません。
php
1if ( false === ( $special_query_results = get_transient( 'special_query_results' ) ) ) {
と
php
1set_transient( 'special_query_results', $special_query_results, 12 * HOUR_IN_SECONDS );
の間にコードを書けば良いわけではなさそうなので
どのように書いたらよいでしょうか。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
単純に global $wpdb; $firstdate = $wpdb->get_var("SELECT post_date from $wpdb->posts where post_status = 'publish' and post_type = 'post' ORDER BY post_date ASC LIMIT 1"); var_dump($firstdate); とすると何が出力されますか?