如何将搜索查询显示为格式化文本?

时间:2015-10-11 作者:730wavy

我试图在搜索页面上显示网站访问者搜索查询。表单有自定义字段,因此不显示是否为“搜索”,也不显示任何自定义字段。

我的搜索表单-

<!-- 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 --> ....................
函数中的我的搜索函数。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\'); }
       }
在我的搜索中。php我有这个显示帖子数量和搜索查询?s=搜索-

<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>
在同一页上,这是我使用的循环--

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


                    <?php
                   endwhile; endif; wp_reset_query();
                    ?>
再一次,我的查询字符串如下所示。。。。但很明显,它会根据用户的标准而变化--

/?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
那么,如何才能将所有搜索值显示为格式化文本呢?

2 个回复
最合适的回答,由SO网友:730wavy 整理而成

这就是我一直在寻找的答案-

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

SO网友:Aric Watson

您可能需要过滤掉一些参数,更改它们的显示顺序,等等,但这应该让您开始:

$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";
}
你可以使用foreach ($_REQUEST as $key => $value) { 除非有特殊原因你不使用$_REQUEST.

相关推荐

如何读取WordPress$Query关联数组(散列)键的值

WordPress编程新手(来自更为传统的环境),并试图了解其一些“独特”特性。我们的网站上有一个目录页,此代码驻留在functions.php, 如果条件为true,则调整结果。if( $query->is_post_type_archive( \'directory\' ) ){ ...//do stuff } 我想知道如何获取is_post_type_archive 这就是“目录”当我对值使用测试时。。。var_dumb($query->is_post