在前端按自定义字段搜索自定义帖子类型

时间:2020-08-26 作者:Sultan khan

我想将过滤器添加到我的网站,并想按自定义字段过滤自定义帖子类型。例如,我有一个名为country的自定义字段,当访问者输入一个国家时,它应该显示该国家的所有自定义帖子类型。我创建了以下代码,但没有显示结果。

    add_shortcode(\'user_search\',\'My_User_search\');
      function My_User_search($atts = null){
      $out = user_search_form();

     if (isset($_GET[\'post\']) && $_GET[\'post\'] == "post" && isset($_GET[\'search_by\'])){
      global $wpdb;
      $metakey = $_GET[\'search_by\'];
      $args = array(\'meta_key\' => $metakey);
    
      if (isset($_GET[\'s_value\'])){
     $metavalue = $_GET[\'s_value\'];
     $args[\'meta_value\'] = $metavalue;
     $arg = (array(\'numberposts\'    => -1,  \'post_type\' => \'umrahpackage\', \'meta_key\' => \'country\', \'meta_value\' =>  $metavalue ));
          
     } 
       
    $search_users = new WP_Query($args); 
    
    $out .= \'<div class="user_search_results">\';
    if( $search_users->have_posts() ) {
        
        
        while ( $search_users->have_posts() ) : $search_users->the_post();
         $out .= \'<div class="title_des edu tthome3">
            <div class="title edu_titl ttshome3"> \'; 
               $out .= \'<li>\' . get_the_title() . \'</li>\';
           $out .= \' </div>
        </div>\';  
      endwhile; 
        
    $out .= \'</div>\';
   }
 }
  return $out;
 }

       //function to display user search form
      function user_search_form(){
      $metavalue = $metakey = \'\';
       if (isset($_GET[\'search_by\'])){
          $metakey = $_GET[\'search_by\'];
       }
     if (isset($_GET[\'s_value\'])){
         $metavalue = $_GET[\'s_value\'];
      }

    $re = \'<div class="user_search"><form action="" name="user_s" method="get">
        <label for="search_by">Search by:</label>
        
        
        
<select id="search_by" name="search_by">\';
if ($metakey != \'\'){
    $re.= \'"\';
    $re.= ($metakey == "country") ;
   
}else{
    $re .= \'
        <option value="country">Comapny Name</option>\';
}
$re .= \'
 </select>
        <label>Company Name</label>
        <input id="s_value" name="s_value" type="text" value="\'.$metavalue.\'"/>
        <input name="post_search" id="user_search" type="hidden" value="search_posts"/>
        <input id="submit" type="submit" value="Search" />
    </form></div>\';
return $re;
}

1 个回复
最合适的回答,由SO网友:Sultan khan 整理而成

我解决了它,如果它对其他人有帮助,我就在这里分享

add_shortcode(\'user_search\',\'My_User_search\');
function My_User_search($atts = null){
$out = user_search_form();
$args = array(\'post_type\' => \'umrahpackage\',\'order\' => \'asc\', 
            \'meta_query\' => array(
              array(
                  \'key\' => \'country\',
                  \'value\' => $_GET[\'s_value\'],
                  \'compare\' => \'Like\',
                  )
              )
          );
    $the_query = new WP_Query( $args );
    if( $the_query->have_posts() ):
        while( $the_query->have_posts() ) : $the_query->the_post();
        
          if($_GET[\'s_value\']==\'\'){
            //before search hide the posts 
          }
          else {
            $out .= \'<li>\' . get_the_title() . \'</li>\';
          }  
        endwhile;
    endif;    
return $out;
}
//function to display user search form
 function user_search_form(){

 $re = \'<div class="user_search"><form action="" name="user_s" method="get">
    <label for="search_by">Search by:</label>
    <div id="search_by" name="search_by">\';

$re .= \'
        <label>Company Name</label>
        <input id="s_value" name="s_value" type="text" value="\'.$metavalue.\'"/>
       
        <input name="user_search" id="user_search" type="hidden" value="umrahpackage"/>
        <input id="submit" type="submit" value="Search" />
    </form></div>\';
return $re;
}

?>

相关推荐

Search query with quotes

我试图在wordpress中的自定义帖子类型中搜索WP\\U查询。我有这个要求$wp_query = Wp_Query([\'post_type\' => \'property\', \'s\' => \'test\']); 这很好用。但当我的财产有带撇号的所有权时test\'s, 它不工作,在我的搜索属性中进入搜索页面后,我得到test\\\'s, 当我试图通过这个属性查找不起作用的帖子时,也当我硬编码更改搜索字符串而不使用反斜杠时。有什么办法解决这个问题吗?使用一些自定义过滤器或