@oookonnn

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

Wordpressでカテゴリとタグの絞り込み検索

解決したいこと

Wordpressでカテゴリとタグの絞り込み検索を実現したいのですがうまく行きません。

実現したいこと

https://**.jp/?s=美容院+かわいい+女性向け

カテゴリ 「美容院」
タグ 「かわいい」「女性向け」を選ぶと上記の配列を渡すのが希望なのですが

発生している問題・エラー

https://**.jp/?s=美容院&s=かわいい&s=女性向け

となってしまい、最後の「女性向け」にか検索結果に表示されません

自分で試したこと

searchform.p

<form role="search" method="get" action="<?php bloginfo('url'); ?>">
<div class="searchlist">
<!--カテゴリ検索ボックス-->
<?php 
$terms = get_terms( 'category', array(
'orderby' => 'name'
) );
if(!is_wp_error($terms)):
    foreach($terms as $term):

$t_check=filter_input(INPUT_GET,'check01',FILTER_DEFAULT,["options" => ["default" => []],"flags" => FILTER_REQUIRE_ARRAY]);
$checked["check01"] = [ $term->name => ""];
foreach((array)$t_check as $val){
$checked["check01"][$val]="checked";
}
?>
<label><input type="checkbox" name="s" id="s" value="<?php echo $term->name; ?>" <?php echo $checked["check01"][$term->name]; ?>><?php echo $term->name . ''; ?></label>
<?php endforeach; endif; ?>
</div>

<div class="searchlist tag">
<!--タグ検索ボックス-->
<?php 
$terms = get_terms( 'post_tag', array(
'orderby' => 'name'
) );
if(!is_wp_error($terms)):
    foreach($terms as $term):

$t_check=filter_input(INPUT_GET,'check02',FILTER_DEFAULT,["options" => ["default" => []],"flags" => FILTER_REQUIRE_ARRAY]);
$checked["check02"] = [ $term->name => ""];
foreach((array)$t_check as $val){
$checked["check02"][$val]="checked";
}
?>
<label><input type="checkbox" name="s" id="s" value="<?php echo $term->name; ?>" <?php echo $checked["check02"][$term->name]; ?>><?php echo $term->name . ''; ?></label>
<?php endforeach; endif; ?>
</div>
<div class="submitbox"><input type="submit" value="この条件で検索する" /></div>
</form>

search.php

            <?php
    global $wp_query;
    $total_results = $wp_query->found_posts;//該当件数を取得
    $search_query = get_search_query();//検索キーワードを取得
    query_posts($query_string.'&posts_per_page=100');//表示件数を指定
?>

<h1 class="page-title"><?php printf( esc_html__( '「%s」の検索結果', 'akamist_s' ), '<span>' . get_search_query() . '</span>' ); ?></h1>
</header><!-- .page-header -->

<div class="inner">
<div class="box_wrap gal">

<?php
    if( $total_results > 0 )://該当ありの場合
    if(have_posts()):
?>
    <?php while(have_posts()): the_post(); ?>
	<div class="gallerybox">
<div class="gallery">
画像取得コード省略
</div>
<p><?php the_title(); ?></p>
<p class="date"><?php the_time('Y.m.d'); ?></p>
</div>
    <?php endwhile; ?>
<?php endif; ?>
<?php else: //該当なしの場合 ?>
<h2><?php echo $search_query; ?>に一致する情報は見つかりませんでした。</h2>
<?php endif; ?>
</div>

どなたかご教授いただけたら幸いです。

0 likes

1Answer

Your answer might help someone💌