###前提・実現したいこと
WordPressの中でアーカイブを作っています。
アコーディオン形式で年をクリックするとスライドダウンして、その年の投稿のある月と件数が表示される仕様です。
また、その月をクリックするとアーカイブへのリンクとなってます。
元々投稿のカテゴリーが一つでしたので
archive.php、category-a.php、single-a.phpに下記コードを記載していました。
###該当のソースコード
php
<div id="archives_year"> <ul class="menu_list"> <li> <?php $year_prev = null; $months = $wpdb->get_results("SELECT DISTINCT MONTH( post_date ) AS month , YEAR( post_date ) AS year, COUNT( id ) as post_count FROM $wpdb->posts WHERE post_status = 'publish' and post_date <= now( ) and post_type = 'post' GROUP BY month , year ORDER BY post_date DESC"); foreach($months as $month) : $year_current = $month->year; if ($year_current != $year_prev){ if ($year_prev != null){?> </ul> <?php } ?> <div class="main_menu"><span><?php echo $month->year; ?>年</span></div> <ul class="sub_menu"> <?php } ?> <li> <a href="<?php bloginfo('url') ?>/date/<?php echo $month->year; ?>/<?php echo date("m", mktime(0, 0, 0, $month->month, 1, $month->year)) ?>"> <?php echo date("n", mktime(0, 0, 0, $month->month, 1, $month->year)) ?>月 (<?php echo $month->post_count; ?>) </a> </li> <?php $year_prev = $year_current; endforeach; ?> </ul> </li> </ul> </div>
###発生している問題・エラーメッセージ
①追加で別のカテゴリーbを作ることになりました。
現在の記述ではカテゴリーaとb両方のアーカイブになっていますが、これを元のaのみにしたいです。
上記ソースのpost_typeの後に次のように書きましたがうまくいきませんでした。BDのテーブルが違うのかと考えてます。
php
and post_type = 'post' and slug = 'a'
②それとは別にイベントというカスタム投稿を作りそちらのアーカイブも作成することになりました。
event organizerというプラグインを使用しています。
https://ja.wordpress.org/plugins/event-organiser/
内容は絞れたのですが、投稿日ではなくイベント日時でのアーカイブにしたいです。
こちらはpost_typeを次のように変更しました。
php
and post_type = 'event'
SQLはそこまで知識がなくこちらのソースも数ヶ月前に調べながらどうにか作りました。
解決方法や別の作り方がありましたら、ご教授のほどよろしくお願いします。
###試したこと
下記ソースにしたところカテゴリーの絞りこみはできたのですが、記事数のカウントが増えてしまいました。
現在1記事のみしか公開していないのですが、4になってしまいます。
php
$year_prev = null; $months = $wpdb->get_results("SELECT DISTINCT MONTH( post_date ) AS month , YEAR( post_date ) AS year, COUNT( id ) as post_count FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON $wpdb->posts.ID = $wpdb->postmeta.post_id LEFT JOIN $wpdb->term_relationships ON $wpdb->posts.ID = $wpdb->term_relationships.object_id LEFT JOIN $wpdb->term_taxonomy ON $wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id LEFT JOIN $wpdb->terms ON $wpdb->term_taxonomy.term_taxonomy_id = $wpdb->terms.term_id WHERE post_status = 'publish' and post_date <= now( ) and post_type = 'post' and slug = 'column' GROUP BY month , year ORDER BY post_date DESC");
###問題②解決方法
下記のようにソースを変更して解決しました。
php
$year_prev = null; $months = $wpdb->get_results("SELECT MONTH( StartDate ) AS month , YEAR( StartDate ) AS year, COUNT( DISTINCT id ) as post_count FROM $wpdb->posts LEFT JOIN $wpdb->eo_events ON $wpdb->posts.ID = $wpdb->eo_events.post_id WHERE post_status = 'publish' and post_date <= now( ) and post_type = 'event' GROUP BY month , year ORDER BY StartDate DESC");
まだ回答がついていません
会員登録して回答してみよう