0

I want to add filter to my site and want to filter custom post type by custom fields for example i have a custom field with name country when a visitor enter a country it should show all custom post type with that country. 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;

}

1 Answer 1

2

I solved it and share it here if its help some one else

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;
}

?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.