• 解決済 eghato

    (@eghato)


    お世話になっております。
    キーワード、カテゴリ、カスタムフィールド値で絞り込み検索機能を作成しました。
    投稿記事の絞り込みに成功したのでカスタム投稿機能に反映させたのですが、その途端にキーワード検索が動かなくなりました。

    キーワードを入力しない状態だと正常に検索をしてくれるのですが、キーワードが入ると記事が全部はじかれます。
    色々と調べたのですが、原因が分からず質問をさせていただいた次第です。

    もし何かお気づきになられましたら、どうかご教授いただけませんでしょうか。
    どうぞお願い致します。

    検索フォームのコードは以下になります。

    <form method="get" id="searchform" action="<?php echo home_url(); ?>">
        <label for="s">検索</label>
        <input type="text" name="s" id="s" placeholder="検索" />
    
        <select name="catnum">
          <option value="" selected>カテゴリを選択</option>
          <?php
          $taxonomy = 'review_category';
          $args = array(
          'hide_empty' => false
          );
          $terms = get_terms( $taxonomy , $args );
    
          foreach ( $terms as $term ) :
          ?>
          <?php
          if($_GET['catnum'] == $term->term_id){
          $term_select ='selected';
          }else{
          $term_select ='';
          }
          ?>
    
          <option value="<?php echo $term->term_id; ?>" <?php echo $term_select ?> ><?php echo $term->name; ?></option>
    
          <?php endforeach; ?>
    
        </select>
    
        <?php
        if($_GET['price'] == '無料'){
          $price_free = 'selected';
        }
        elseif($_GET['price'] == '有料'){
          $price_charge = 'selected';
        }
        else{
          $price_select = 'selected';
        }
        ?>
    
        <select name="price">
            <option value="" <?php echo $price_select ?> >価格を選択</option>
            <option value="無料" <?php echo $price_free ?> >無料</option>
            <option value="有料" <?php echo $price_charge ?> >有料</option>
        </select>
    
        <?php
        if($_GET['cusos'] == 'iPad'){
          $cusos_iPad = 'selected';
        }
        elseif($_GET['cusos'] == 'iPhone'){
          $cusos_iPhone = 'selected';
        }
        elseif($_GET['cusos'] == 'android'){
          $cusos_android = 'selected';
        }
        elseif($_GET['cusos'] == 'win'){
          $cusos_win = 'selected';
        }
        else{
          $cusos_select = 'selected';
        }
        ?>
    
        <select name="cusos">
            <option value="" <?php echo $cusos_select ?> >OSを選択</option>
            <option value="iPhone" <?php echo $cusos_iPhone ?> >iPhone</option>
            <option value="iPad" <?php echo $cusos_iPad ?> >iPad</option>
            <option value="android" <?php echo $cusos_android ?> >android</option>
            <option value="win" <?php echo $cusos_win ?> >win</option>
        </select>
    
        <?php
        if($_GET['sort'] == 'new'){
          $sort_new = 'selected';
        }
        elseif($_GET['sort'] == 'old'){
          $sort_old = 'selected';
        }
        elseif($_GET['sort'] == 'popular'){
          $sort_popular = 'selected';
        }
        else{
          $sort_select = 'selected';
        }
        ?>
    
        <select name="sort">
          <option value="select" <?php echo $sort_select ?> >記事の順番を選択</option>
          <option value="new" <?php echo $sort_new ?> >新着順</option>
          <option value="old" <?php echo $sort_old ?> >古い順</option>
          <option value="popular" <?php echo $sort_popular ?> >人気順</option>
        </select>
    
        <input type="submit" value="検索" />
    </form>

    検索機能部分のコードは以下になります。

    <?php
        $s = $_GET['s'];
        $catnum = $_GET['catnum'];
        $cusos = $_GET['cusos'];
        $price = $_GET['price'];
    
        if($catnum){
          $taxquerysp[] = array(
                  'taxonomy'=>'category',
                  'terms'=> $catnum,
                  'field'=>'term_id',
                  'operator'=>'AND'
                  );
      }
      $taxquerysp['relation'] = 'AND';
    
      if($cusos){
              $metaquerysp[] = array(
                      'key'=>'os',
                      'value'=> $cusos,
                      'operator'=>'AND'
                      );
          }
      if($price){
              $metaquerysp[] = array(
                      'key'=>'price',
                      'value'=> $price,
                      'operator'=>'AND'
                      );
          }
      $metaquerysp['relation'] = 'AND';
    
      if (isset($_GET['sort'])) {
        if($_GET['sort'] == 'new'){
          $args = array(
            'order' => 'DESC',
            'orderby' => 'date',
            'tax_query' => $taxquerysp,
            'meta_query' => $metaquerysp,
            's' => $s,
            'post_type' => 'review'
          );
        }
        elseif($_GET['sort'] == 'old'){
          $args = array(
            'order' => 'ASC',
            'orderby' => 'date',
            'tax_query' => $taxquerysp,
            'meta_query' => $metaquerysp,
            's' => $s,
            'post_type' => 'review'
          );
        }
        elseif($_GET['sort'] == 'popular'){
          $args = array(
          'meta_key' => 'views',
          'orderby' => 'meta_value_num',
          'tax_query' => $taxquerysp,
          'meta_query' => $metaquerysp,
          's' => $s,
          'post_type' => 'review'
          );
        }
        else{
          $args = array(
            'order' => 'DESC',
            'orderby' => 'date',
            'tax_query' => $taxquerysp,
            'meta_query' => $metaquerysp,
            's' => $s,
            'post_type' => 'review'
          );
        }
    }
    
       ?>
        <?php $posts = new wp_query($args); ?>
      <?php if ($posts->have_posts()) : while ($posts->have_posts()) : $posts->the_post(); ?>

1件の返信を表示中 - 1 - 1件目 (全1件中)
  • トピック投稿者 eghato

    (@eghato)

    一から作りなおしたら正常に動きました。
    お騒がせして申し訳ありませんでした。

1件の返信を表示中 - 1 - 1件目 (全1件中)
  • トピック「カスタム投稿記事のキーワード検索ができない。」には新たに返信することはできません。