Wordpressの検索機能を実装しているのですが実現できず困っています。
2019/03/11-17:26
最新のソースコードに修正しました。
■実装したい事
1.フリーワード検索(実装済み)
2.クソノミー、ターム絞り込み検索(実装済み)
3.カスタムフィールドの絞り込み検索(未実装)
フリーワード検索は独立していていいのですが、投稿タイプ内でカテゴリ、カスタムフィールドを全て検索対象にして、
絞り込み検索できるようにしたいです。
タブでカテゴリの要素を出したものと複数のカスタムフィールドが混在している少しごちゃごちゃした検索項目の出し方です。
カスタムフィールド単体では検索にヒットするようになりましたが複数検索させるやり方がわかりません。
主に以下の参考サイトより自分の知識で出来る範囲で試してみましたが駄目でした。
https://www.webtoolnavi.com/wordpress-search-everything/
http://wpcj.net/1363
https://izizm.net/webizm/web-management/wordpress/1754/
http://kotori-blog.com/wordpress/refinement_search/
https://sheeplog.work/acf_custom_search_hudosan/
以下現時点でのソースです。
■search.php
<?php $s = $_GET['s']; $my_taxonomy = $_GET['my_taxonomy']; $term_slug = $_GET['term_slug']; $and_or = $_GET['and_or']; $market = $_GET['market']; $use = $_GET['use']; if($term_slug){ $tax_query[] = array( 'taxonomy'=> $my_taxonomy, 'terms'=> $term_slug, 'include_children'=> false, 'field'=> 'slug', 'operator'=> $and_or ); } $args = array( 'tax_query' => $tax_query, 's' => $s, 'posts_per_page' => -1, 'meta_query' => array( 'relation'=>'OR', 'market_car' => array( 'value' => '$market[0]', 'compare' => 'LIKE', ), 'cat_use' => array( 'value' => $use[0], 'compare' => 'LIKE', ), ) ); $wp_query= null; $the_query = new WP_Query( $args ); ?> <?php if ( $the_query->have_posts() ) : ?> <section id="archive_area"> <ul class="item_list"> <!-- ループはじめ --> <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?> <li><a href="<?php the_permalink(); ?>"> <p class="tl"><?php the_title(); ?></p> </a></li> <?php endwhile; ?> <!-- ループおわり --> </ul> </section> <?php wp_reset_postdata(); ?> <?php else : ?> <p>お探しの記事はありませんでした。</p> <?php endif; ?>
■searchform.php
<form role="search" method="get" id="searchform" class="search-form" action="<?php echo home_url('/test/'); ?>"> <div class="free_serach"> <div class="input_box"> <input type="text" class="field" name="s" id="s" placeholder="入力してください。" value="<?php the_search_query(); ?>"> <input type="image" src="<?php echo site_url(); ?>/wp-content/uploads/product/ico_search_01.png" class="submit" name="submit" value="検索"> </div> </div> <section id="tabArea"> <?php $my_taxonomy = 'test_cat'; $cat_args = array( 'parent' => 0, //トップレベルのタームのみ 'hierarchical' => 0 //子タームを含めない ); $i = 0; $terms = get_terms($my_taxonomy,$cat_args); ?> <ul id="tabMenu"> <li><a href="#cat00" class="open">カテゴリ01</a></li> <li><a href="#cat01">カスタムフィールド01</a></li> <li><a href="#cat02">カスタムフィールド02</a></li> </ul> <!-- フォームの追加はじめ --> <div id="cat00"> <div class="btn_box"> <?php foreach( $terms as $term ): $child_cats = get_terms($my_taxonomy,'hierarchical=0&hide_empty=1&parent='.$term -> term_id); if($child_cats): ?> <?php foreach($child_cats as $child_cat): ?> <?php $child_cat_name = esc_html($child_cat -> name); $target_cat_slug = esc_html($child_cat->slug); ?> <label><input type="checkbox" name="term_slug[]" value="<?php echo $child_cat->slug; ?>"><span><?php echo $child_cat->name; ?></span></label> <?php endforeach;?> <?php endif; ?> <?php wp_reset_postdata(); ?> <?php endforeach; ?> </div> </div> <div id="cat01"> <div class="btn_box"> <label><input type="checkbox" name="market[]" value="market_01"><span>カスタムフィールドの項目</span></label> <label><input type="checkbox" name="market[]" value="market_02"><span>カスタムフィールドの項目</span></label> <label><input type="checkbox" name="market[]" value="market_03"><span>カスタムフィールドの項目</span></label> <label><input type="checkbox" name="market[]" value="market_04"><span>カスタムフィールドの項目</span></label> </div> </div> <div id="cat02"> <div class="btn_box"> <label><input type="checkbox" name="use[]" value="use_01"><span>カスタムフィールドの項目</span></label> <label><input type="checkbox" name="use[]" value="use_02"><span>カスタムフィールドの項目</span></label> <label><input type="checkbox" name="use[]" value="use_03"><span>カスタムフィールドの項目</span></label> <label><input type="checkbox" name="use[]" value="use_04"><span>カスタムフィールドの項目</span></label> </div> </div> <input type="hidden" name="my_taxonomy" value="<?php echo $my_taxonomy ?>"> <select name="and_or" id="and_or"> <option value="AND">AND</option> <option value="IN">OR</option> </select> <!-- フォームの追加おわり --> <input type="submit" class="search-submit" value="<?php echo esc_attr_x( 'Search', 'submit button' ) ?>" /> <input type="hidden" name="post_type" id="post_type" value="product"> </section> </form>
■Advanced Custom Fields登録例
フィールド名:cf_market market_01 : テキスト01 market_02 : テキスト02 market_03 : テキスト03 market_04 : テキスト04 フィールド名:cf_use use_01 : テキスト01 use_02 : テキスト02 use_03 : テキスト03 use_04 : テキスト04
不足情報等ありましたら何なりとお申し付け下さい。
すみませんが反応して頂けたらとても助かります。
どうぞよろしくお願い致します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/03/10 12:27
2019/03/10 12:45
2019/03/10 15:40
2019/03/11 02:24
2019/03/14 14:32
2019/03/18 06:08