不動産サイトのような絞り込み検索
フォーム側
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
<form method="get" id="searchform" action="http://kotori-blog.com/test/"> <label for="s" class="assistive-text">検索</label> <input type="text" class="field" name="s" id="s" placeholder="検索" /> <select name="catnum" style="margin-top:20px;"> <option value="" selected>カテゴリ</option> <?php $categories = get_categories('parent=0'); foreach($categories as $category) : ?> <option value="<?php echo $category->term_id; ?>"><?php echo $category->cat_name; ?></option> <?php endforeach; ?> </select> <select name="placenum" style="margin-top:20px;"> <option value="" selected>間取りタイプ(カスタムタクソノミー)</option> <?php $taxonomy_name = 'place'; $taxonomys = get_terms($taxonomy_name); if(!is_wp_error($taxonomys) && count($taxonomys)): foreach($taxonomys as $taxonomy): $tax_posts = get_posts(array('post_type' => get_post_type(), 'taxonomy' => $taxonomy_name, 'term' => $taxonomy->slug ) ); if($tax_posts): ?> <option value="<?php echo $taxonomy->slug; ?>"><?php echo $taxonomy->name; ?></option> <?php endif; endforeach; endif; ?> </select> <select name="type" style="margin-top:20px;"> <option value="" selected>ルームタイプ(カスタムフィールドのセレクトボックス)</option> <option value="アパート">アパート</option> <option value="マンション">マンション</option> <option value="一戸建て・その他">一戸建て・その他</option> </select> <div style="margin-top:20px">家賃(カスタムフィールドのテキストフィールド)</div> <select name="low" style="margin-top:5x;"> <option value="0" selected>下限なし</option> <option value="20000">20,000円以上</option> <option value="25000">25,000円以上</option> <option value="30000">30,000円以上</option> <option value="35000">35,000円以上</option> <option value="40000">40,000円以上</option> <option value="45000">45,000円以上</option> <option value="50000">50,000円以上</option> <option value="55000">55,000円以上</option> <option value="60000">60,000円以上</option> <option value="65000">65,000円以上</option> <option value="70000">70,000円以上</option> <option value="75000">75,000円以上</option> <option value="80000">80,000円以上</option> </select> ~<br /> <select name="high" style="margin-top:5px;"> <option value="20000">20,000円以下</option> <option value="25000">25,000円以下</option> <option value="30000">30,000円以下</option> <option value="35000">35,000円以下</option> <option value="40000">40,000円以下</option> <option value="45000">45,000円以下</option> <option value="50000">50,000円以下</option> <option value="55000">55,000円以下</option> <option value="60000">60,000円以下</option> <option value="65000">65,000円以下</option> <option value="70000">70,000円以下</option> <option value="75000">75,000円以下</option> <option value="80000">80,000円以下</option> <option value="9999999" selected>上限なし</option> </select> <div style="margin-top:20px">こだわり(タグ)</div> <?php $taxonomy_name = 'post_tag'; $taxonomys = get_terms($taxonomy_name); if(!is_wp_error($taxonomys) && count($taxonomys)): foreach($taxonomys as $taxonomy): $tax_posts = get_posts(array('post_type' => get_post_type(), 'taxonomy' => $taxonomy_name, 'term' => $taxonomy->slug ) ); if($tax_posts): ?> <label><input type="checkbox" name="post_tag[]" value="<?php echo $taxonomy->slug; ?>"><?php echo $taxonomy->name; ?></label><br> <?php endif; endforeach; endif; ?> <div>こだわり(カスタムフィールドのチェックボックス)</div> <label><input type="checkbox" name="kodawari[]" value="駐車場あり">駐車場あり</label><br> <label><input type="checkbox" name="kodawari[]" value="バス・トイレ別">バス・トイレ別</label><br> <label><input type="checkbox" name="kodawari[]" value="ペット相談">ペット相談</label><br> <label><input type="checkbox" name="kodawari[]" value="2階以上住戸">2階以上住戸</label> <input type="submit" class="submit" name="submit" id="searchsubmit" value="検索" /> </form> |
検索結果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
<?php $s = $_GET['s']; $catnum = $_GET['catnum']; $placenum = $_GET['placenum']; $type = $_GET['type']; $low = $_GET['low']; $high = $_GET['high']; $post_tag = $_GET['post_tag']; $kodawari = $_GET['kodawari']; if($catnum){ $taxquerysp[] = array( 'taxonomy'=>'category', 'terms'=> $catnum, 'include_children'=>false, 'field'=>'term_id', 'operator'=>'AND' ); } if($post_tag){ $taxquerysp[] = array( 'taxonomy'=>'post_tag', 'terms'=> $post_tag, 'include_children'=>false, 'field'=>'slug', 'operator'=>'AND' ); } if($placenum){ $taxquerysp[] = array( 'taxonomy'=>'place', 'terms'=> $placenum, 'include_children'=>false, 'field'=>'slug', 'operator'=>'AND' ); } $taxquerysp['relation'] = 'AND'; if($kodawari){ foreach($kodawari as $val){ $metaquerysp[] = array( 'key'=>'kodawari', 'value'=> $val, ); } } if($type){ $metaquerysp[] = array( 'key'=>'type', 'value'=> $type, 'compare'=>'=', ); } $metaquerysp[] = array( 'key'=>'price', 'value'=>array( $low, $high ), 'compare'=>'BETWEEN', 'type'=>'NUMERIC', ); $metaquerysp['relation'] = 'AND'; ?> <?php if($s){ ?>検索キーワード:<?php echo $s; ?><br><?php } ?> <?php if($catnum){ ?>カテゴリ:<?php echo get_cat_name($catnum); ?><br><?php } ?> <?php if($placenum){ ?>間取りタイプ:<?php echo get_term_by('slug',$placenum,"place")->name; ?><br><?php } ?> <?php if($type){ ?>ルームタイプ:<?php echo $type; ?><br><?php } ?> <?php if($low == 0 && $high == 9999999){}else{ ?>家賃:<?php if($low == 0){ ?>下限なし<?php }else{ echo number_format($low).'円'; } ?> ~ <?php if($high == 9999999){ ?>上限なし<?php }else{ echo number_format($high).'円';} ?><br> <br><?php } ?> <?php if (is_array($cutnum)) { ?>カテゴリ:<?php foreach($cutnum as $val){ if ($val === end($cutnum)) { echo get_cat_name($catnum); }else{ echo get_cat_name($catnum).", "; } } } ?> <?php if (is_array($post_tag)) { ?>こだわり(タグ):<?php foreach($post_tag as $val){ if ($val === end($post_tag)) { echo get_term_by('slug',$val,"post_tag")->name; }else{ echo get_term_by('slug',$val,"post_tag")->name.", "; } } ?><br><br><?php } ?> <?php if (is_array($kodawari)) { ?>こだわり(カスタムフィールドのチェックボックス):<?php if (is_array($kodawari)) { foreach($kodawari as $val){ if ($val === end($kodawari)) { echo $val; }else{ echo $val.", "; } } } } ?> <?php query_posts( array( 'tax_query' => $taxquerysp, 'meta_query' => $metaquerysp, 's' => $s, ) ); ?> <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <div><?php the_title(); ?></div> <div><?php the_content(); ?></div> <?php endwhile; else : ?> 該当なし <?php endif;wp_reset_query(); ?> |
ページネーション
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
<?php $s = $_GET['s']; $catnum = $_GET['catnum']; $placenum = $_GET['placenum']; $type = $_GET['type']; $low = $_GET['low']; $high = $_GET['high']; $post_tag = $_GET['post_tag']; $kodawari = $_GET['kodawari']; if($catnum){ $taxquerysp[] = array( 'taxonomy'=>'category', 'terms'=> $catnum, 'include_children'=>false, 'field'=>'term_id', 'operator'=>'AND' ); } if($post_tag){ $taxquerysp[] = array( 'taxonomy'=>'post_tag', 'terms'=> $post_tag, 'include_children'=>false, 'field'=>'slug', 'operator'=>'AND' ); } if($placenum){ $taxquerysp[] = array( 'taxonomy'=>'place', 'terms'=> $placenum, 'include_children'=>false, 'field'=>'slug', 'operator'=>'AND' ); } $taxquerysp['relation'] = 'AND'; if($kodawari){ foreach($kodawari as $val){ $metaquerysp[] = array( 'key'=>'kodawari', 'value'=> $val, ); } } if($type){ $metaquerysp[] = array( 'key'=>'type', 'value'=> $type, 'compare'=>'=', ); } $metaquerysp[] = array( 'key'=>'price', 'value'=>array( $low, $high ), 'compare'=>'BETWEEN', 'type'=>'NUMERIC', ); $metaquerysp['relation'] = 'AND'; ?> <?php if($s){ ?>検索キーワード:<?php echo $s; ?><br><?php } ?> <?php if($catnum){ ?>カテゴリ:<?php echo get_cat_name($catnum); ?><br><?php } ?> <?php if($placenum){ ?>間取りタイプ:<?php echo get_term_by('slug',$placenum,"place")->name; ?><br><?php } ?> <?php if($type){ ?>ルームタイプ:<?php echo $type; ?><br><?php } ?> <?php if($low == 0 && $high == 9999999){}else{ ?>家賃:<?php if($low == 0){ ?>下限なし<?php }else{ echo number_format($low).'円'; } ?> ~ <?php if($high == 9999999){ ?>上限なし<?php }else{ echo number_format($high).'円';} ?><br> <br><?php } ?> <?php if (is_array($cutnum)) { ?>カテゴリ:<?php foreach($cutnum as $val){ if ($val === end($cutnum)) { echo get_cat_name($catnum); }else{ echo get_cat_name($catnum).", "; } } } ?> <?php if (is_array($post_tag)) { ?>こだわり(タグ):<?php foreach($post_tag as $val){ if ($val === end($post_tag)) { echo get_term_by('slug',$val,"post_tag")->name; }else{ echo get_term_by('slug',$val,"post_tag")->name.", "; } } ?><br><br><?php } ?> <?php if (is_array($kodawari)) { ?>こだわり(カスタムフィールドのチェックボックス):<?php if (is_array($kodawari)) { foreach($kodawari as $val){ if ($val === end($kodawari)) { echo $val; }else{ echo $val.", "; } } } } ?> <?php query_posts( array( 'tax_query' => $taxquerysp, 'meta_query' => $metaquerysp, 's' => $s, 'posts_per_page' => 1, 'paged' => $paged, ) ); ?> <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <div><?php the_title(); ?></div> <div><?php the_content(); ?></div> <?php endwhile; else : ?> 該当なし <?php endif; ?> <?php wp_pagenavi(); //ページ送り wp_reset_query(); ?> |
引用元: