絞り込み検索結果でページネーションが機能しなかった
WordPressでカスタムタクソノミーのタームで絞り込み検索をかけた際にページネーションがうまくいかなかった現象が出ました。
チェックボックスでタームを検索する以外に、キーワード検索も実装していて
両方から同じsearch.phpに検索結果を表示させるようにしていました。
//functions.php <?php //-----------------ページネーション----------------- function pagination( $pages, $paged, $range = 3, $show_only = true ) { $pages = ( int ) $pages; $paged = $paged ?: 1; if ( $show_only && $pages === 1 ) { echo '<div><ul><li>1</li></ul></div>'; return; } if ( $pages === 1 ) return; if ( 1 !== $pages ) { echo '<div><ul>'; if ( $paged > $range + 1 ) { echo '<li><a href="', get_pagenum_link(1) ,'"><i class="fas fa-fast-backward"></i></a></li>'; } if ( $paged > 1 ) { echo '<li><a href="', get_pagenum_link( $paged - 1 ) ,'" ><i class="fas fa-caret-left"></i></a></li>'; } for ( $i = 1; $i <= $pages; $i++ ) { if ( $i <= $paged + $range && $i >= $paged - $range ) { if ( $paged === $i ) { echo '<li>', $i ,'</li>'; } else { echo '<li><a href="', get_pagenum_link( $i ) ,'">', $i ,'</a></li>'; } } } if ( $paged < $pages ) { echo '<li><a href="', get_pagenum_link( $paged + 1 ) ,'"><i class="fas fa-caret-right"></i></a></li>'; } if ( $paged + $range < $pages ) { echo '<li><a href="', get_pagenum_link( $pages ) ,'"><i class="fas fa-fast-forward"></i></a></li>'; } echo '</ul></div>'; } } ?>
//search.php <?php $searchType = $_GET['searchType']; $termCheck = $_GET['termCheck']; if($searchType == 'keyword'){ $search_query = new WP_Query( array( 'post_type' => get_query_var( 'post_type', 'custom' ) , 'paged' => get_query_var( 'paged', 1 ) , 's' => get_search_query() , 'posts_per_page' => 5 ) ); }elseif($searchType == 'checkBox'){ $search_query = new WP_Query( array( 'post_type' => get_query_var( 'post_type', 'custom' ) , 'paged' => get_query_var( 'paged', 1 ) , 'posts_per_page' => 5 , 'tax_query' => array( array( 'taxonomy' => 'taxonomy', 'field' => 'slug', 'terms' => $termStack, 'operator' => 'IN' ) ) ) ); } if ( $search_query->have_posts() ) : while ( $search_query->have_posts() ) : $search_query->the_post(); /* ~ 検索結果出力 ~ */ endwhile; endif; wp_reset_postdata(); //ページネーション部分 if ( function_exists( 'pagination' ) ) : pagination( $search_query->max_num_pages, get_query_var( 'paged' ) ); endif; ?>
//searchformFreeWord.php <div> <form method="get" class="search_container" action="<?php echo home_url('/'); ?>"> <input type="text" name="s" value="<?php the_search_query(); ?>" placeholder="キーワードを入力" /> <input type="hidden" name="searchType" value="keyword"> <input type="hidden" name="post_type" value="custom"> <input type="hidden" name="termCheck[]" value=""> <button type="submit" class="search_btn" value="検索する"> </form> </div>
//searchformCheckBox.php <form method="get" action="<?php echo home_url('/'); ?>"> <input type="hidden" name="s" value=""> <input type="hidden" name="searchType" value="checkBox"> <input type="hidden" name="post_type" value="custom"> <div> <input type="checkbox" name="termCheck[]" value="term1" id="term1"> <label for="term1">term1</label> <input type="checkbox" name="termCheck[]" value="term2" id="term2"> <label for="term2">term2</label> <input type="checkbox" name="termCheck[]" value="term3" id="term3"> <label for="term3">term3</label> </div> <button type="submit" class="search_btn" value="検索する">検索する </form>
キーワード検索の場合ではページネーションが機能していたのですが、
タームの絞り込み検索ではうまくいきませんでした。
原因はキーワード検索用に使用していたプラグイン「Search Everything」でした。
kotori-blog.com
このプラグインを使用してカスタムフィールド内にもキーワード検索ができるようにしていました。
しかし、プラグインを導入すると、キーワードを空で検索した際に全件表示ではなく全件非表示になってしまいます。
それが原因でタームで絞り込んだ際にキーワードを空に設定したことによってページネーションがうまく機能しなかった可能性が出てきました。
キーワードが空で検索した際の挙動の修正を行います。
プラグインの入っているフォルダにある「search-everything.php」を修正します。
~~/wp-content/plugins/search-everything/search-everything.php
の階層にファイルは存在します。
279行目付近に
//search-everything.php <?php if ( $searchQuery != '' ) { // lets use _OUR_ query instead of WP's, as we have posts already included in our query as well(assuming it's not empty which we check for) $where = " AND ((" . $searchQuery . ")) "; } ?>
という箇所があると思いますが
これを修正します。
//search-everything.php <?php if ( $searchQuery != '' && $searchQuery != '()' ) { // lets use _OUR_ query instead of WP's, as we have posts already included in our query as well(assuming it's not empty which we check for) $where = " AND ((" . $searchQuery . ")) "; } ?>
キーワードが空の場合には「$searchQuery」は「''」空文字ではなく、「'()'」を返します。
「 '()'」を返す場合の記述がないので空の場合に全件取得する挙動になっています。
これを修正することにより、希望通りに検索結果を取得することができました。
以下、参考サイト
wordpress.org