2016-08-25 246 views
0

我在页面中有三种不同的搜索形式。产品,餐厅和网页是不同的帖子类型。试图通过隐藏的输入类型在search.php中显示结果。它没有显示出我期望的结果。我的代码中的某些内容是错误的。请提供建议或建议。Wordpress搜索三种不同的形式

形式1

<form role="search" method="get" action="<?php bloginfo('url'); ?>"> 
<input type="text" value="" name="s" id="s" /> 
<input type="hidden" value="Restaurant" name="post_type" /> 
<input type="submit" id="searchsubmit" value="Search" /> 
</form> 

形式2

<form role="search" method="get" action="<?php bloginfo('url'); ?>"> 
<input type="text" value="" name="s" id="s" /> 
<input type="hidden" value="Products" name="post_type" /> 
<input type="submit" id="searchsubmit" value="Search" /> 
</form> 

形式3

<form role="search" method="get" action="<?php bloginfo('url'); ?>"> 
         <input type="text" name="search" placeholder="Search..."> 
         <input type="hidden" name="post_type" value="Pages" /> 
        </form> 

以下是我的search.php结构

$myvalue = $_GET['post_type']; 

     if ($myvalue == "Restaurant"){ 
      echo "res"; 

      function SearchFilter($query) { 
       if ($query->is_search) { 
        $query->set('post_type', 'sanha-restau'); 
       } 
       return $query; 
      } 

      add_filter('pre_get_posts','SearchFilter'); 

     if (have_posts()) : 
      while (have_posts()) : the_post(); 
       echo '<ul><li><strong>'; 
       echo get_the_title() . '</strong><br> ('; 
       echo substr(get_the_excerpt(), 0, 200) . ') '; 
       echo '</li></ul>';    
      endwhile; 
      else : 
       get_template_part('content', 'none'); 
     endif; 


     } else if ($myvalue == "Products"){ 
      echo "pro"; 

      function SearchFilter($query) { 
       if ($query->is_search) { 
        $query->set('post_type', 'sanha-product'); 
       } 
       return $query; 
      } 

      add_filter('pre_get_posts','SearchFilter'); 

     if (have_posts()) : 
      while (have_posts()) : the_post(); 
       echo '<ul><li><strong>'; 
       echo get_the_title() . '</strong><br> ('; 
       echo substr(get_the_excerpt(), 0, 200) . ') '; 
       echo '</li></ul>';    
      endwhile; 
      else : 
       get_template_part('content', 'none'); 
     endif; 


     } else if ($myvalue == "Pages"){ 
      echo "pages"; 

      if (have_posts()) : 
       while (have_posts()) : the_post(); 
        echo '<ul><li><strong>'; 
        echo get_the_title() . '</strong><br> ('; 
        echo substr(get_the_excerpt(), 0, 200) . ') '; 
        echo '</li></ul>';    
       endwhile; 
       else : 
        get_template_part('content', 'none'); 
      endif; 

     } 

请指教。感谢

+0

请提供您的反馈,如果你找到答案非常有帮助:) – Noman

回答

0

通过检查你的代码似乎你想在3种文章类型

Products, Restaurants and Pages

按文档,你可以使用数组post_type在多个岗位类型进行搜索,以搜索。

其中WP_QUERY

Show posts based on a keyword search. 

s (string) - Search keyword. 
Show Posts based on a keyword search 

Display posts that match the search term "abc": 

s参数:我用abc搜索,你可以把它改成esc_sql($_REQUEST['s'])

$searchQuery = new WP_Query(array(
    'post_type' => array('products', 'restaurants','page'), 
    's' => 'abc' // <<-- change this to make it work --> esc_sql($_REQUEST['s']) 
)); 

if ($searchQuery->have_posts()) : 
    while ($searchQuery->have_posts()) : $searchQuery->the_post(); 
     echo '<ul><li><strong>'; 
     echo get_the_title() . '</strong><br> ('; 
     echo substr(get_the_excerpt(), 0, 200) . ') '; 
     echo '</li></ul>';    
    endwhile; 
    else : 
     get_template_part('content', 'none'); 
endif; 

所以会有搜索,无需一个查询使用add_filter。 以上WP_Query将在3种帖子类型中搜索关键字。

0
function load_custom_search_template(){ 
    if(isset($_REQUEST['custom_search'])){ 
     require('search.php'); 
     die(); 
    } 
} 
add_action('init','load_custom_search_template'); 

这个代码工作与所有

,并按照search.php中的代码

$myquery = ''; 
function searchfilter($query) { 
    if ($query->is_search && !is_admin()) { 
     if(isset($_GET['post_type'])) { 
      $type = $_GET['post_type']; 
       if($type == 'sanha-product') { 
        $query->set('post_type',array('sanha_product')); 
        $myquery = 'Products Found For : '; 
       } 
       elseif($type == 'sanha-restau'){ 
        $query->set('post_type',array('sanha_restau')); 
        $myquery = 'Resaurants Found For : '; 
       } 
       elseif($type == 'pages'){ 
        $query->set('post_type',array('pages')); 
        $myquery = 'Search Result For : '; 
       } 
     }  
    } 
return $query; 
} 
add_filter('pre_get_posts','searchfilter');