-2

I am trying to display a site vistors search query on the search page. The form has custom fields so is not showing if it's "Search" and it doesnt show none of my custom fields.

My search form -

<!-- Search Form -->
<section class="searchMain sm2">
<form method="get" id="searchform2" action="<?php echo home_url(); ?>/">
<input type="hidden" class="form-control" name="s" id="s" value="<?php _e('Search', 'framework'); ?>" />
<div class="search-con">

<!-- Search Row 1 -->
<div class="full-divsn">

<!-- City Taxonomy Search -->
<div class="search-field myFields-lg">
<label>City</label>
<select name="property_city" class="form-control">
<?php
$terms = get_terms( "city-type", array( 'hide_empty' => 0 ) );
 $count = count($terms);
 if ( $count > 0  ){
echo "<option class='button' value='City'>All</option>";
     foreach ( $terms as $term ) {
         echo "<option class='button' value='" . $term->slug . "'>" . $term->name . "</option>";
     }
 }
?>
  </select>
</div>
<!-- End City Taxonomy Search -->

<!-- Search State Custom Field -->
<div class="search-field myFields-xxmd">
<label>State</label>
<select name="property_location" class="form-control">
<option value="Any">Any</option>
  <?php
  $metakey = 'imic_property_site_state';
  $statez = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = %s ORDER BY meta_value ASC", $metakey) );
  if ($statez) {
    foreach ($statez as $states) {
      echo "<option value=\"" . $states . "\">" . $states . "</option>";
    }
  }
  ?>
  </select>
</div>
<!-- End Search State Custom Field -->

<!-- Search Price Custom Field -->
<div class="search-field myFields-md">
 <label><?php _e('Price', 'framework'); ?></label>
                                <input type="text" name="min_price" class="form-control" placeholder="<?php _e('Any', 'framework'); ?>">
                          </div>
<div class="search-field myFields-txt">
<label>&nbsp;</label>
<div class="form-control">to</div>
</div>

<div class="search-field myFields-xmd-n">
                                <label>&nbsp;</label>
                                <input type="text" name="max_price" class="form-control" placeholder="<?php _e('Any', 'framework'); ?>">
</div>
<!-- End Search Price Custom Field -->

</div>
<!-- End Search Row 1 --> ....................

My search function in functions.php --

/* Search Filter
      ================================= */   
if(!function_exists('imic_search_filter')){
    function imic_search_filter($query) {
        if($query->is_search()) {
            $property_contract_type=$property_type=$property_neighborhood=$property_location=$property_city=$beds=$baths=$min_price=$max_price=$min_area=$max_area=$ag_value=$ag_expertr='';
            $property_contract_type = isset($_GET['property_contract_type'])?$_GET['property_contract_type']:"";
            $property_contract_type= ($property_contract_type == __('Contract', 'framework')) ? '' :$property_contract_type;
            $property_type = isset($_GET['property_type'])?$_GET['property_type']:'';
            $property_type= ($property_type == __('Any', 'framework')) ? '' :$property_type;
$property_neighborhood = isset($_GET['neighborhood'])?$_GET['neighborhood']:'';
            $property_neighborhood = ($property_neighborhood == __('Neighborhood', 'framework')) ? '' :$property_neighborhood;
            $property_location = isset($_GET['property_location'])?$_GET['property_location']:'';
            $property_location = ($property_location == __('Any', 'framework')) ? '' : $property_location;
            $property_city = isset($_GET['property_city'])?$_GET['property_city']:'';
            $property_city = ($property_city == __('City', 'framework')) ? '' :$property_city;
            $beds = isset($_GET['beds'])?$_GET['beds']:'';
            $beds = ($beds == __('Any', 'framework')) ? '' : $beds;
            $baths = isset($_GET['baths'])?$_GET['baths']:'';
            $baths = ($baths == __('Any', 'framework')) ? '' : $baths;
            $min_price = isset($_GET['min_price'])?$_GET['min_price']:'';
            $min_price = ($min_price == __('Any', 'framework')) ? '' :$min_price;
            $max_price = isset($_GET['max_price'])?$_GET['max_price']:'';
            $max_price = ($max_price == __('Any', 'framework')) ? '' :$max_price;
            $min_area = isset($_GET['min_area'])?$_GET['min_area']:'';
            $min_area = ($min_area == __('Any', 'framework')) ? '' :$min_area;
            $max_area = isset($_GET['max_area'])?$_GET['max_area']:'';
            $max_area = ($max_area == __('Any', 'framework')) ? '' :$max_area;
// Agent Property Value and Ratings
 $apvr = isset($_GET['apvr'])?$_GET['apvr']:'';
            $apvr = ($apvr == __('Any', 'framework')) ? '' :$apvr;
 $apor = isset($_GET['apor'])?$_GET['apor']:'';
            $apor = ($apor == __('Any', 'framework')) ? '' :$apor;
           $id = isset($_GET['id'])?$_GET['id']:'';
            $pincode = isset($_GET['pincode'])?$_GET['pincode']:'';
            $address = isset($_GET['address'])?$_GET['address']:'';
            // If the default text is in the box
            if (!empty($property_contract_type)||!empty($property_type)|| !empty($property_location) || !empty($baths) ||!empty($beds)||(!empty($min_price)||!empty($max_price))||(!empty($min_area)||!empty($max_area))||!empty($apvr)||!empty($apor)||!empty($id)||!empty($pincode)||!empty($address)||!empty($property_city)||!empty($property_neighborhood)) {
                 $s = $_GET['s'];
                 $meta_query=array();
            if ($s == __('Search', 'framework')) {
                $query->set('s', '');
            }
              $query->set('post_type', 'property');
              $query->set('post_status','publish');
              if (!empty($property_type)) {
               $query->set('property-type', $property_type);
                }
                if (!empty($property_city)) {
               $query->set('city-type',$property_city);
                }
if (!empty($property_neighborhood)) {
               $query->set('neighborhood',$property_neighborhood);
                }
              if (!empty($property_contract_type)) {
                 $query->set('property-contract-type', $property_contract_type);
                }
             if (!empty($baths)) {
                     array_push($meta_query, array(
                            'key' => 'imic_property_baths',
                            'value' => $baths,
                             'type' => 'numeric',
                            'compare' => '>='
                        ));
                }
                if (!empty($beds)) { 
                    array_push($meta_query,array(
                            'key' => 'imic_property_beds',
                            'value' => $beds,
                            'type' => 'numeric',
                            'compare' => '>='
                        ));
                }
            if(!empty($min_price)&&!empty($max_price)){
             array_push($meta_query,array(
         'key' =>'imic_property_price',
             'value'=>array($min_price,$max_price),
             'type' =>'numeric',
         'compare'=> 'BETWEEN'
            ));
            }
            else{
               if(!empty($min_price)){
             array_push($meta_query,array(
         'key' =>'imic_property_price',
             'value'=>$min_price,
             'type' =>'numeric',
         'compare'=> '>='
            ));
            }
            if(!empty($max_price)){
             array_push($meta_query,array(
         'key' =>'imic_property_price',
             'value'=>$max_price,
             'type' =>'numeric',
         'compare'=> '<='
            ));
            }
            }if(!empty($min_area)&&!empty($max_area)){
              array_push($meta_query,array(
         'key' => 'imic_property_area',
             'value' => array($min_area,$max_area),
             'type' => 'numeric',
         'compare' => 'BETWEEN'
            ));
            }
            else{
                if(!empty($min_area)){
                array_push($meta_query,array(
         'key' => 'imic_property_area',
             'value' => $min_area,
             'type' => 'numeric',
         'compare' => '>='
            ));
            }
            if(!empty($max_area)){
                array_push($meta_query,array(
         'key' => 'imic_property_area',
             'value' => $max_area,
             'type' => 'numeric',
         'compare' => '<='
            ));
            }
            }
             if (!empty($property_location)) {
                 array_push($meta_query,array(
                'key' => 'imic_property_site_state',
                'value' => $property_location
            ));
            }
 if (!empty($apvr)) {
                 array_push($meta_query,array(
                'key' => 'imic_property_apvr',
                'value' => $apvr
            )); 
}
 if (!empty($apor)) {
                 array_push($meta_query,array(
                'key' => 'imic_property_apor',
                'value' => $apor
            ));       
            }
            if (!empty($id)) {
              array_push($meta_query,array(
                'key' => 'imic_property_site_id',
                'value' => $id,
                 'compare'=>'LIKE'
            ));
             }
             if (!empty($pincode)) {
                array_push($meta_query,array(
                'key' => 'imic_property_pincode',
                'value' => $pincode
            ));
            }
              if (!empty($address)) {
                array_push($meta_query,array(
                'key' => 'imic_property_site_address',
                'value' => $address,
                'compare' => 'LIKE',
            ));
            }
            $query->set('meta_query',$meta_query);
            }else {
               $s = $_GET['s'];
                 if ($s == __('Search', 'framework')) {
                      $query->set('s', '');
                $query->set('post_type', 'property');
            }else{
             $query->set('post_type', 'property');    
            } }

            }
          return $query;
    }
# Add Filters
if(!is_admin()) {
   add_filter('pre_get_posts', 'imic_search_filter'); }
       }

On my search.php I have this displaying number of posts and the search query for ?s=Search -

<h4><?php $num = $wp_query->post_count; if (have_posts()) : ?>You have <?php echo $num; ?> search results in <?php echo get_search_query(); ?><?php endif;?></h4>

And on that same page, this is the loop I use --

<?php
                    if (have_posts()) : while (have_posts()):the_post();?>
                        <?php get_template_part('search','property'); ?>


                    <?php
                   endwhile; endif; wp_reset_query();
                    ?>

And once again My query string looks something like this.... but obviously it changes depending on the users criteria --

/?s=Search&property_city=las-vegas&property_location=Nevada&min_price=20000&max_price=500000&beds=2&baths=3&min_area=1000&max_area=1000000&property_type=apartment&s=Search&apor=Any&apvr=Any&post_type=property

So How can I get all of the search values to display as formatted text?

  • 1
    echo isset( $_GET["s"] ) ? esc_attr( $_GET["s"] ) : ''; ? – Samuel Elh Oct 11 '15 at 10:31
  • Huh? Is that an answer? That seems like it will only get the default search input query and not the custom fields and taxonomies. – Rich Oct 11 '15 at 14:46
  • Then, reformat your question to make it more clear. Look into codex.wordpress.org/Class_Reference/wpdb as it might help you query the fields from your actual database tables.. – Samuel Elh Oct 11 '15 at 15:43
  • 1
    Close voting as you obviously have some plugin or proprietary code at work and without knowing how it works it is impossible to answer the question. Please edit the question and add relevant details about how the search works and what exactly do you want to display. – Mark Kaplun Oct 11 '15 at 16:19
  • 1
    I havn't down voted it, but it is realy still not clear what is it that you want to be displayed. On the face of it the first comment is what you want. If you want to split it into the parameters then it should be trivial to do like the answer sugests.. – Mark Kaplun Oct 12 '15 at 11:38
1

You'll probably want filter out some parameters, change the order they're displayed, etc - but this should get you started:

$s = "/?s=Search&property_city=las-vegas&property_location=Nevada&min_price=20000&max_price=500000&beds=2&baths=3&min_area=1000&max_area=1000000&property_type=apartment&s=Search&apor=Any&apvr=Any&post_type=property";
parse_str($s,$parts);
foreach ($parts as $key => $value) {
    $name = ucwords(str_replace("_"," ",$key));
    echo "$name: $value<br />\n";
}

You can probably just use foreach ($_REQUEST as $key => $value) { unless there's a specific reason you're not using $_REQUEST.

1

This is the answer that I was looking for an helped me -

echo isset( $_GET["s"] ) ? esc_attr( $_GET["s"] ) : '';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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