【WordPress】日付検索的な何か

電脳備忘録

本記事のソースコードの利用によって生じた損害について、当方は一切の責任を負いません。ご自身の判断と責任のもとで参照・ご利用ください。

この記事は最終更新から3年以上経過しています。

日付として保存しているわけではなく、テキスト内の日付を元に検索をかける必要があったのですが、まぁなんとかできましたよと...。

リンクをクリックしたら、結果を生じさせると、でwordpressはarchive-event.phpテンプレートを使うようです。これはあらかじめ用意しておいたんですけどね。
テンプレートを読む順序が決まっていることは知ってはいるのですが、この辺の仕様がまだいまいちよくわからない...。

<ul>
    <li><a href="/event/index.php?month='04月'">4月</a></li>
    <li><a href="/event/index.php?month='05月'">5月</a></li>
    <li><a href="/event/index.php?month='06月'">6月</a></li>
    <li><a href="/event/index.php?month='07月'">7月</a></li>
    <li><a href="/event/index.php?month='08月'">8月</a></li>
    <li><a href="/event/index.php?month='09月'">9月</a></li>
    <li><a href="/event/index.php?month='10月'">10月</a></li>
    <li><a href="/event/index.php?month='11月'">11月</a></li>
    <li><a href="/event/index.php?month='12月'">12月</a></li>	
    <li><a href="/event/index.php?month='01月'">1月</a></li>
    <li><a href="/event/index.php?month='02月'">2月</a></li>
    <li><a href="/event/index.php?month='03月'">3月</a></li>
  </ul>
  

archive-event.phpはこんな感じで

<?php
  $get_date = $_GET['month']; 
  $args = array(
      'post_type'  => 'event',
      'meta_query' => array(
          array(
              'key'     => 'conference_time',
              'value'   => $get_date,
              'compare' => 'LIKE'
          )
      )
  );
  $query = new WP_Query( $args );
  ?>
  <table>
  <tr>
    <th>イベント</th>
    <th>開催日</th>
    <th>開催時間</th>
  </tr>
  <?php
  $get_date = $_GET['month']; 
  if (!isset($get_date)) :?>
  
  <?php if ( have_posts() ) : ?>
  <?php
  /* Start the Loop */
  while ( have_posts() ) : the_post(); ?>
  <tr>
  <td><a href="<?php echo $getURL = the_permalink(); ?>"><?php print($post ->post_title); ?></a></td><td><?php print($post ->event_date); ?></td><td><?php print($post ->event_time); ?></td>
  </tr>
  <?php
  endwhile;
  twentytwelve_content_nav( 'nav-below' );
  ?>
  <?php else : ?>
        <?php get_template_part( 'content', 'none' ); ?>
  <?php endif; ?>
  
  <?php else : ?>
  <?php while ( $query ->have_posts() ) : $query ->the_post(); ?>
  <tr>
  <td><a href="<?php echo $getURL = the_permalink(); ?>"><?php print($post ->post_title); ?></a></td><td><?php print($post ->event_date); ?></td><td><?php print($post ->event_time); ?></td>
  </tr>
  <?php endwhile; ?>
  <?php wp_reset_query(); ?>
  <?php endif; ?>
  </table>

意図したとおりに抽出できているっぽいので、まぁ良しとしよう...。

0%