2019-02-06 13:02:50 PHP

PHP

Copy Copied! Full
<?php /** * The template for displaying archive pages * * @link https://developer.wordpress.org/themes/basics/template-hierarchy/ * * @package horeru */ get_header(); ?> <?php if (have_posts()) { $matched_ids = array(); $post_ids = array(); while (have_posts()) { the_post(); array_push($matched_ids, get_the_ID()); } $matched_ids_str = 'post_id = "' . implode('" OR post_id = "', $matched_ids) .'"'; // ------------------------SQL構築------------------------------------------- if (!empty($_GET['word']) || !empty($_GET['week']) || !empty($_GET['time'])) { global $wpdb; //------------------ FreeWord検索用SQL ------------------------------------ if (!empty($_GET['word'])) { $word = $_GET['word']; $word = '%' . $wpdb->esc_like($word) . '%'; $post_ids_from_posts = $wpdb->get_col($wpdb->prepare(" SELECT DISTINCT ID FROM {$wpdb->posts} WHERE post_title LIKE '%s' OR post_content LIKE '%s' ", $word, $word)); $post_ids_from_terms = $wpdb->get_col($wpdb->prepare(" SELECT DISTINCT object_id FROM {$wpdb->term_relationships} WHERE term_taxonomy_id IN ( SELECT tt.term_id FROM {$wpdb->term_taxonomy} AS tt INNER JOIN {$wpdb->terms} AS tm ON tt.term_id = tm.term_id WHERE tm.name LIKE '%s' ) ", $word)); $post_ids_from_metas = $wpdb->get_col($wpdb->prepare(" SELECT DISTINCT post_id FROM {$wpdb->postmeta} WHERE meta_value LIKE '%s' ", $word)); $word_data = array_merge($post_ids_from_posts, $post_ids_from_terms, $post_ids_from_metas); $matched_ids = array_intersect($matched_ids, $word_data); } // ----------------------------------------------------------------------- // ----------------------営業時間用SQL-------------------------------------- $week = array("sun", "mon", "tue", "wed", "thu", "fri", "sat"); // week有り if (!empty($_GET['week'])) { $week = array_diff($week, $_GET['week']); $week = 'meta_key = "' . implode('" OR meta_key = "', $week) . '"'; $week_time_data = $wpdb->get_results(" SELECT post_id, meta_key, meta_value FROM {$wpdb->postmeta} WHERE ({$week}) AND ({$matched_ids_str}) "); foreach ($week_time_data as $day_time_data) { $day_time_data->meta_value = unserialize($day_time_data->meta_value); } // week有り--time有り if (!empty($_GET['time'])) { $time_option = array(); $num = array_sum($_GET['time']); $num_1 = $num % 2; $num_2 = (($num - $num_1) / 2) % 2; $num_3 = (($num - $num_1 - $num_2 * 2) / 4) % 2; if ($num_1 == 1) { array_push($time_option, 1); } else { array_push($time_option, 0); } if ($num_2 == 1) { array_push($time_option, 1); } else { array_push($time_option, 0); } if ($num_3 == 1) { array_push($time_option, 1); } else { array_push($time_option, 0); } $time_data = array(); foreach ($week_time_data as $day_time_data) { for ($i=0; $i < 3; $i++) { if ($time_option[$i] == 1) { if ($day_time_data->meta_value[$i] == 1) { array_push($time_data, $day_time_data->post_id); } } } } // week有り--time無し } else { $time_data = array(); foreach ($week_time_data as $day_time_data) { if ($day_time_data->meta_value[0] != 0 || $day_time_data->meta_value[1] != 0 || $day_time_data->meta_value[2] != 0) { array_push($time_data, $day_time_data->post_id); } } } $time_data = array_unique($time_data); // week無し } else { $week = 'meta_key = "' . implode('" OR meta_key = "', $week) . '"'; $week_time_data = $wpdb->get_results(" SELECT post_id, meta_key, meta_value FROM {$wpdb->postmeta} WHERE ({$week}) AND ({$matched_ids_str}) "); foreach ($week_time_data as $day_time_data) { $day_time_data->meta_value = unserialize($day_time_data->meta_value); } // week無し--time有り if (!empty($_GET['time'])) { $time_option = array(); $num = array_sum($_GET['time']); $num_1 = $num % 2; $num_2 = (($num - $num_1) / 2) % 2; $num_3 = (($num - $num_1 - $num_2 * 2) / 4) % 2; if ($num_1 == 1) { array_push($time_option, 1); } else { array_push($time_option, 0); } if ($num_2 == 1) { array_push($time_option, 1); } else { array_push($time_option, 0); } if ($num_3 == 1) { array_push($time_option, 1); } else { array_push($time_option, 0); } $time_data = array(); foreach ($week_time_data as $day_time_data) { for ($i=0; $i < 3; $i++) { if ($time_option[$i] == 1) { if ($day_time_data->meta_value[$i] == 1) { array_push($time_data, $day_time_data->post_id); } } } } // week無し--time無し } else { $time_data = array(); foreach ($week_time_data as $day_time_data) { if ($day_time_data->meta_value[0] != 0 || $day_time_data->meta_value[1] != 0 || $day_time_data->meta_value[2] != 0) { array_push($time_data, $day_time_data->post_id); } } } $time_data = array_unique($time_data); } // ----------------------------------------------------------------------- $matched_ids = array_intersect($matched_ids, $time_data); } // ------------------------------------------------------------------------- if ($matched_ids) { $args = array( 'post_type' => 'shops', 'post_status' => 'publish', 'post__in' => $matched_ids, ); $query = new WP_Query($args); } ?>
RECOMMEND
<?php date_default_timezone_set('Asia/Tokyo'); $now = strtotime(date('Y-m-d H:i')); /* * 以下の表示を自動で切り替えるタイマー * ・ヘッダーのボタン(参加登録 or 新歓情報 or SNS) * ・参加登録ページの内容 * ・お問い合わせページの新歓お問い合わせに関する文言 * ・9月1日に行うトップページ支援団体の切り替え * ・トップ and 開催概要のフォーラム終了に関する文言切り替え(開催いたします → 開催いたしました) */ // ▼ 参加登録,新歓のタイマー ▼ // 変数 "state" に入る文字列により,ヘッダーのボタンと参加登録・お問い合わせページの表示を切り替え // strtotime 内にその年の参加登録,新歓の期間を適切に入れる // 【注意】"strtotime内"の書式は知識がない場合は変更しないこと('20xx-xx-xx yy:yy' の形式のまま。スペースなども削除しない) if ($now >= strtotime('2017-08-15 0:00') && $now <= strtotime('2017-09-10 23:59')) { // 参加登録の期間 $state = 'registration'; } elseif ($now >= strtotime('2019-01-01 0:00') && $now <= strtotime('2019-05-10 23:59')) { // 新歓の期間 $state = 'fresh'; } else { // 上記以外の期間 $state = 'default'; } /* * 【簡単な説明】 * 変数 "state" に "registration, fresh, default" のいずれかを代入することで条件分岐させている。 * registration ー 参加登録期(フォームを公開している期間) * fresh ー 新歓期(だいたい新歓の受付を始めてから,合宿の締切まで。その年による) *
PHP