如何在wordpress中的自定义帖子类型页面中执行搜索

时间:2017-11-07 05:47:46

标签: php wordpress search

我的常见问题解答页面中有一个搜索栏。

<div class="input-group">
                        <input class="form-control" placeholder="Search Question Here" type="text">
                        <span class="input-group-btn">
                        <button class="btn btn-search" type="button"><i class="fa fa-search"></i></button>
                        </span> 
                    </div>

下面列出了所有问题,当用户输入单词时,应显示相应的结果。这是我的页面的其余部分,

$args=array('post_type'=>'questions','post_status'=>'publish','posts_per_page'=>-1,'order'=>'DESC');
$questions=get_posts($args);

<div class="row">
            <div class="col-md-3">
                <h3 class="faq-cat"><?= $cat->name;?> ?</h3>
            </div>
        <div class="col-md-9" role="tablist">
               <?php 
               foreach($cat_posts as $catqn)
               {
                // var_dump($catqn);
               ?>

                <div class="panel panel-default">
                  <div class="panel-heading" role="tab">
                    <h4 class="panel-title question-heading">
                      <a role="button" data-toggle="collapse" data-parent="#accordion" href="#<?php echo $catqn->ID; ?>" aria-expanded="false" aria-controls="collapseOne" class="collapsed">
                       <?php echo get_post_meta($catqn->ID,'question',true);?> <strong>?</strong>                        
                        <span class="faq-btn faq-btn-minus hidden-xs"><i class="fa fa-caret-down"></i></span>
                        <span class="faq-btn faq-btn-plus hidden-xs"><i class="fa fa-caret-up"></i></span></a>
                    </h4>
                  </div>
                  <div id="<?php echo $catqn->ID; ?>" class="panel-collapse collapse" role="tabpanel">
                    <div class="panel-body">
                      <p><?php echo get_post_meta($catqn->ID,'answer',true);?></p>
                    </div>
                  </div>
                </div>
                <?php
                }
                ?>                

             </div>
        </div>
        <?php 
      }
      ?>

        </div>             
    </div>
</div>

任何人都可以指导我完成这个吗? ?

3 个答案:

答案 0 :(得分:0)

需要在搜索表单中添加隐藏的自定义帖子类型输入。

<form class="search" action="<?php echo home_url( '/' ); ?>">
                            <input type="search" name="s" placeholder="Search Question Here">
                            <input type="submit" value="Search"  class="btn btn-search">
                            <input type="hidden" name="post_type" value="questions">
                    </form>

答案 1 :(得分:0)

<div class="input-group">
                        <form method="post">
                        <input class="form-control" name="searchx" placeholder="Search Question Here" type="text">
                        <span class="input-group-btn">
                        <button class="btn btn-search" name="submit" type="button"><i class="fa fa-search"></i></button>
                        </span> 
                        </form>
                    </div>
               <?php      
           if(isset($_POST['submit'])){   
                 $searchword = $_POST['searchx'];
$args=array('post_type'=>'questions','post_status'=>'publish','posts_per_page'=>-1,'s' => $searchword,'order'=>'DESC');
$cat_posts=get_posts($args);
echo "<pre>";print_r($cat_posts);echo "</pre>";
?>

<div class="row">
            <div class="col-md-3">
            </div>
        <div class="col-md-9" role="tablist">
               <?php 
               foreach($cat_posts as $catqn)
               {
                // var_dump($catqn);
               ?>

                <div class="panel panel-default">
                  <div class="panel-heading" role="tab">
                    <h4 class="panel-title question-heading">
                      <a role="button" data-toggle="collapse" data-parent="#accordion" href="#<?php echo $catqn->ID; ?>" aria-expanded="false" aria-controls="collapseOne" class="collapsed">
                       <?php echo get_post_meta($catqn->ID,'question',true);?> <strong>?</strong>                        
                        <span class="faq-btn faq-btn-minus hidden-xs"><i class="fa fa-caret-down"></i></span>
                        <span class="faq-btn faq-btn-plus hidden-xs"><i class="fa fa-caret-up"></i></span></a>
                    </h4>
                  </div>
                  <div id="<?php echo $catqn->ID; ?>" class="panel-collapse collapse" role="tabpanel">
                    <div class="panel-body">
                      <p><?php echo get_post_meta($catqn->ID,'answer',true);?></p>
                    </div>
                  </div>
                </div>
                <?php
                }
                ?>                

             </div>
        </div>
        <?php 
           }
      ?>

        </div>             
    </div>
</div>

请试试这个。

答案 2 :(得分:0)

你可以试试这个,

    <form class="search" action="<?php echo home_url( '/' ); ?>">
      <input type="search" name="s" placeholder="Search...">
      <input type="submit" value="Search">
      <input type="hidden" name="post_type" value="questions">
    </form>

您可以使用操作pre_get_posts

使用操作来修改搜索查询
<?php 

function my_pre_get_posts($query) {

   if( is_admin() ) 
      return;

   if( is_search() && $query->is_main_query() ) {
       $query->set('post_type', 'questions');
   } 

 }

add_action( 'pre_get_posts', 'my_pre_get_posts' );

&GT;

希望这会对你有帮助。