Wordpress4.9.1です。見よう見まねで実装しようとしているので、説明が不明瞭な部分があるかもしれません。また、PHP初心者のため、コードも冗長な部分や見づらい部分があろうかと思います。申し訳ございません。
###前提・実現したいこと
投稿を複数のカスタムタクソノミーで絞込したいと考えています。
タクソノミーは親子階層を持っています。
親を選択したときは子をすべて含めて検索、
子を選択したときは子のみを検索したいです。
子を含む投稿のタクソノミーは親と子を指定しています。
###発生している問題・エラーメッセージ
現在、親を選択すると検索結果に現れず、子を選択したときのみ検索結果が表示されます。
もともと子がない親の場合は正常に検索結果が表示されます。
###該当のソースコード
検索条件設定画面での記述は下記の通りです。
下記のproductは子あり、foodは子なしです。
PHP
1 2<?php 3<input type="search" class="search-field" placeholder="キーワードを入力" value="<?php echo get_search_query() ?>" name="s" /> 4 5//products 子あり 6$terms_order = get_terms ( "products", array ( 'orderby' => 'term_order', 'order' => 'ASC' ) ); 7 8// 親タームと子供のタームに分解 9foreach ( $terms_order as $term ) { 10 if ( $term -> parent == "0" ) { 11 $parents_object[] = $term; 12 } else { 13 $children_object[] = $term; 14 } 15} 16 17// 親カテゴリー(親ターム) 18foreach ( $parents_object as $parent ) { 19 if ( isset ( $_POST["products"] ) ) { 20 if ( in_array ( $parent -> slug, $_POST["products"] ) ) { 21 $checked_text = " checked"; 22 } else { 23 $checked_text = ""; 24 } 25 } 26 27 echo '<li>' . '<input type="checkbox" name="products[]" value="' . $parent -> slug . '"' . $checked_text . '>' . $parent -> name . '</li>'; 28 29 // 子カテゴリー(子ターム) 30 $child_flg = ""; 31 foreach ( $children_object as $child ) { 32 if ( $parent -> term_id == $child -> parent ) { 33 34 if ( isset ( $_POST["products"] ) ) { 35 if ( in_array ( $child -> slug, $_POST["products"] ) ) { 36 $checked_text = " checked"; 37 } else { 38 $checked_text = ""; 39 } 40 } 41 42 echo '<li class="child">' . '<input type="checkbox" name="products[]" value="' . $child -> slug . '"' . $checked_text . '>' . $child -> name . " " . '</li>'; 43 $child_flg = 1; 44 } 45 } 46} ?> 47</ul> 48<ul> 49 50//food 子なし 51$tax_name = 'food'; 52$terms_order2 = get_terms($tax_name); 53if(!is_wp_error($terms_order2) && count($terms_order2)): 54 foreach($terms_order2 as $taxonomy): 55 $tax_posts = get_posts(array('post_type' => get_post_type(), 'taxonomy' => $tax_name, 'term' => $taxonomy->slug ) ); 56 if($tax_posts): 57?> 58<li><input type="checkbox" name="food[]" value="<?php echo $taxonomy->slug; ?>"><?php echo $taxonomy->name; ?></label></li> 59<?php 60 endif; 61 endforeach; 62endif; 63?> 64</ul>
親子で見た目を区別したいため、上記のように振り分けています。
検索結果ページには下記の記述をしています。
PHP
1<?php 2$s = $_GET['s']; 3$tax_products = $_GET['products']; 4$tax_food = $_GET['food']; 5 6if(count($tax_products) >= 1){$tax_products_flg = 1;} else {$tax_products_flg = 0;}; 7if(count($tax_food) >= 1){$tax_food_flg = 1;} else {$tax_food_flg = 0;}; 8 9if($tax_products_flg + $tax_food_flg > 1){$relation_key = 'AND';} else {$relation_key = 'OR';} 10 11$search_taxonomies = array( 12 'relation' => $relation_key, 13 array( 14 'taxonomy' => 'products', 15 'field' => 'slug', 16 'terms' => $tax_products, 17 'operator' => 'AND' 18 ), 19 array( 20 'taxonomy' => 'food', 21 'field' => 'slug', 22 'terms' => $tax_food, 23 'operator' => 'IN' 24 ) 25 ); 26?> 27<?php 28query_posts( array( 29 'tax_query' => $search_taxonomies, 30 's' => $s 31 ) 32); 33 34?> 35 36<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> 37 38<div><?php the_title(); ?></div> 39<div><?php the_content(); ?></div> 40 41<?php endwhile; else : ?> 42 43<div>該当なし</div> 44 45<?php endif; 46wp_reset_query(); ?>
$search_taxonomiesをvar_dumpで見てみたのですが、
子ありの表示されない時と、子なしの表示される時で差異はありませんでした。(入っているタクソノミーは異なりますが)
ちなみに検索ワードも認識されません。
var_dumpで$sを見ると、ちゃんと入力したものが表示されています…。
色々WEB検索してみたりして調べてみたのですが、ここ数日どうにも行き詰まっております…。
どこが原因なのかもサッパリ見当がつかず…。
お心当たりの方、ヒントなどでもお教えいただければ幸いです。
以上、何卒よろしくお願い致します。
###その後チェックした事項
-$wp_query->requestの中身
↓下記はうまく表示できないときの中身です。
SQL
1string(436) "SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1 AND ( 2 wp_term_relationships.term_taxonomy_id IN (3,4,5) 3) AND wp_posts.post_type IN ('post', 'page', 'attachment') AND (wp_posts.post_status = 'publish' OR wp_posts.post_author = 1 AND wp_posts.post_status = 'private') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10"
↓下記は表示できるときの中身です。
SQL
1string(432) "SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1 AND ( 2 wp_term_relationships.term_taxonomy_id IN (4) 3) AND wp_posts.post_type IN ('post', 'page', 'attachment') AND (wp_posts.post_status = 'publish' OR wp_posts.post_author = 1 AND wp_posts.post_status = 'private') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10"
差異は「wp_term_relationships.term_taxonomy_id IN (3,4,5)」の数値だけでした。
該当DB内にちゃんと数値が存在するかも確認しました。
-フォームのname属性のタイプミス確認
→何度か見直しましたが、タイプミスはありませんでした。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。