search custom post type by custom fields on front end

Question

I want to add filter to my site and want to filter custom post type by custom fields

I have created the following code but not showing result.

    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;

}

0
, , Sultan khan 2 years 2020-08-26T16:10:20-05:00 0 Answers 45 views 0

Leave an answer

Browse
Browse