前提・実現したいこと
カスタム投稿(omairi)のカスタムフィールドで入力した値をもとにsearchform.php/search.phpで検索システムを作成しようと思っています。
条件がカスタム投稿(omairi)で入力した記事だけ表示すること。←これはできました。
参照したい値がカスタム投稿の投稿名、カスタムフィールドで指定したカスタムタクソノミー(animal_type)、この2つの値です。
search.phpにてカスタムタクソノミー(animal_type)のドロップダウンリスト、投稿名・本文で検索できる入力欄を表示しています。
問題なのは、カスタムタクソノミー(animal_type)のドロップダウンリストだけを選択して検索した場合、カスタムタクソノミー・投稿名両方を指定した場合は想定通りの記事が表示されるのですが、投稿名のみで検索した場合検索結果が0件になってしまいます。
投稿名のみで検索した場合に想定した記事が表示されるようにしたいです。
searchform.php
PHP
1<form id="form" action="<?php echo esc_url(home_url('/')); ?>" method="get"> 2<label for="animal_type_select">種類検索</label> 3<?php wp_dropdown_categories('taxonomy=animal_type&name=animal_type&value_field=slug&hide_empty=0&show_option_none=- - -&id=animal_type_select'); ?> 4<label for="s_name">お名前検索</label> 5<input id="s_name" name="s" type="search" placeholder="お名前" value="<?php the_Search_query(); ?>"/> 6 <input type="hidden" name="post_type" value="omairi"> 7 <button type="submit" id="s_btn">検索</button> 8</form>
search.php
PHP
1 2<?php get_header(); ?> 3 4<?php $term_slug = $_GET['animal_type']; 5 $animal_type = get_animal_type($term_slug); 6 $search_query = get_search_query(); 7 $total_results = $wp_query->found_posts; 8 if(empty($animal_type)) { 9 $result = $search_query; 10 } 11 elseif(empty($search_query)) { 12 $result = $animal_type; 13 } 14 else { 15 $result = $animal_type . '・' . $search_query; 16 } 17 $result = "「" . $result . "」" . "の検索結果"; 18 ?> 19 20<div id="breadcrumb" class="clearfix"> 21 <ul class="contents_inner"> 22 <li><a href="<?php echo esc_url(home_url('/'))?>">HOME</a></li> 23 <li><a href="<?php echo esc_url(home_url('archives/omairi/'))?>">お参り広場</a></li> 24 <li><?php echo $result ?></li> 25 </ul> 26</div> 27 28 <div id="lower_img"> 29 <div class="contents_inner"> 30 <h2 class="min mt">お参り広場</h2> 31 <img src="<?php echo get_template_directory_uri(); ?>/img/area/title_bog.png" class="title_bog"> 32 </div> 33 <p class="boder_line"></p> 34 </div> 35 36 <!--検索窓呼び出し--> 37 <?php get_search_form(); ?> 38 39 <section id="omairi_archive"> 40 <div class="contents_inner clearfix"> 41 <h3 class="search_query"><?php echo $result ?>/<?php echo $total_results; ?>件</h3> 42 43 <?php if($search_query || $animal_type): ?> 44 <?php if (have_posts()): ?> 45 <?php while (have_posts()): the_post(); ?> 46 <?php //変数定義 47 $img = get_field('omairi_img'); 48 $place = get_field('omairi_place'); 49 $background = get_field('omairi_background'); 50 $term_slug = $_GET['animal_type']; 51 $animal_type = get_animal_type($term_slug); 52 ?> 53 54 <article class="omairi_box omairi_box_<?php echo $background;?>"> 55 <img src="<?php echo $img; ?>" alt="<?php echo get_the_title(); ?>"> 56 <p class="animal_type">type:<?php echo $animal_type; ?></p> 57 <p class="name">name:<?php echo get_the_title(); ?></p> 58 <p class="place">place:<?php echo $place; ?></p> 59 <a class="detail_btn" href="<?php the_permalink(); ?>">お参りする</a> 60 </article> 61 62 <?php endwhile; ?> 63 <?php else: ?> 64 <p class="not_found">検索キーワードに該当する記事がありませんでした。</p> 65 <?php endif; ?> 66 <?php else: ?> 67 <p class="not_found">検索キーワードを指定してください。</p> 68 <?php endif; ?> 69 70 </div> 71 </section>
試したこと
searchform.phpのwp_dropdown_categories部分を削除したら投稿名の検索はうまくいきます。
どなたかわかる方いらっしゃいましたらよろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー