WordPressの検索で、複数のカテゴリを使って検索する方法を調べました。
サンプルコード
デフォルトの投稿で、カテゴリーとカスタムタクソノミー(genre)を使った検索を実装してみます。
PHP
<form method="get" action="<?php echo home_url(); ?>">
<input type="hidden" name="s">
<input type="hidden" name="post_type" value="post">
<p>カテゴリー</p>
<?php $categories = get_categories(); if( $categories ): ?>
<ul class="select">
<li>
<input type="radio" name="cat" id="cat" value="">
<label for="cat">
すべて
</label>
</li>
<?php foreach( $categories as $category ): ?>
<li>
<input type="radio" name="cat" id="cat-<?php echo esc_attr( $category->term_id ); ?>" value="<?php echo esc_attr( $category->term_id ); ?>">
<label for="cat-<?php echo esc_attr( $category->term_id ); ?>">
<?php echo esc_html( $category->name ); ?>
</label>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<p>ジャンル</p>
<?php $terms = get_terms( 'genre' ); if( $terms ): ?>
<ul class="select">
<li>
<input type="radio" name="genre" id="genre" value="">
<label for="genre">
すべて
</label>
</li>
<?php foreach( $terms as $term ): ?>
<li>
<input type="radio" name="genre" id="genre-<?php echo esc_attr( $term->term_id ); ?>" value="<?php echo esc_attr( $term->slug ); ?>">
<label for="genre-<?php echo esc_attr( $term->term_id ); ?>">
<?php echo esc_html( $term->name ); ?>
</label>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<input type="submit" id="searchsubmit" value="検索">
</form>
基本的には以前投稿したWordPressの検索機能を実装してみると同じで、カスタムタクソノミーを検索で使用したい場合は「?タクソノミー名(genre)=スラッグ名」のような形になるようにすればOKです。
【参考サイト】
コメントが承認されるまで時間がかかります。