从搜索查询中排除隐藏的输入值

时间:2016-04-01 12:00:16

标签: php wordpress

我一直在寻找我的问题的解决方案,但我找不到一个。我有一个wordpress网站,其中我有两个搜索表单。搜索表单根据页面参数显示。两种搜索表单都隐藏了具有不同值的输入字段。因此,两种形式都根据隐藏的输入值转到各自的搜索功能。

问题是隐藏的输入值也包含在搜索查询中,导致不显示任何结果。所以,我想从搜索查询中排除这个隐藏的输入字段。所以请帮帮我。 这是搜索表单的代码。

<form class="form-inline" id="searchPropertyForm" role="search" method="get" action="<?php echo esc_url($search_submit); ?>">
            <input type="hidden" name="sort" id="sort" value="newest" />
            <div class="form-group hidden-xs adv property-search">
                    <a href="javascript:void(0);" data-toggle="dropdown" class="btn btn-white dropdown-toggle cat">
                        <span class="dropdown-label"><?php esc_html_e('Property Name', 'reales'); ?></span>&nbsp;&nbsp;&nbsp;<span class="caret"></span>
                    </a>
                    <ul class="dropdown-menu dropdown-select">
                        <li class="active"><input type="radio" name="search_name" value="0" checked="checked"><a href="javascript:void(0);"><?php esc_html_e('Property Name', 'reales'); ?></a></li>
                        <li><input type="radio" name="search_name" value="The Golf Residence at Al Hamra Village"><a href="javascript:void(0);">The Golf Residence at Al Hamra Village</a></li>
                        <li><input type="radio" name="search_name" value="The Duplex at Al Hamra Village"><a href="javascript:void(0);">The Duplex at Al Hamra Village</a></li>
                        <li><input type="radio" name="search_name" value="‎Townhouses at Al Hamra Village"><a href="javascript:void(0);">‎Townhouses at Al Hamra Village</a></li>
                        <li><input type="radio" name="search_name" value="The Villas at Al Hamra Village"><a href="javascript:void(0);">The Villas at Al Hamra Village</a></li>
                        <li><input type="radio" name="search_name" value="The Marina Residence at Al Hamra Village"><a href="javascript:void(0);">The Marina Residence at Al Hamra Village</a></li>
                        <li><input type="radio" name="search_name" value="The Royal Breeze Residence"><a href="javascript:void(0);">The Royal Breeze Residence</a></li>
                        <li><input type="radio" name="search_name" value="The Bab Al Bahr Residence"><a href="javascript:void(0);">The Bab Al Bahr Residence</a></li>
                        <li><input type="radio" name="search_name" value="Bayti Townhomes"><a href="javascript:void(0);">Bayti Townhomes</a></li>
                        <li><input type="radio" name="search_name" value="Bayti Premium"><a href="javascript:void(0);">Bayti Premium</a></li>
                    </ul>
            </div>
            <div class="form-group hidden-xs adv">
                    <a href="javascript:void(0);" data-toggle="dropdown" class="btn btn-white dropdown-toggle cat">
                        <span class="dropdown-label"><?php esc_html_e('Category', 'reales'); ?></span>&nbsp;&nbsp;&nbsp;<span class="caret"></span>
                    </a>
                    <ul class="dropdown-menu dropdown-select">
                        <li class="active"><input type="radio" name="search_p_cat" value="0" checked="checked"><a href="javascript:void(0);"><?php esc_html_e('Category', 'reales'); ?></a></li>
                        <li><input type="radio" name="search_p_cat" value="Residence"><a href="javascript:void(0);">Residence</a></li>
                        <li><input type="radio" name="search_p_cat" value="Town House"><a href="javascript:void(0);">Town House</a></li>
                        <li><input type="radio" name="search_p_cat" value="Villas"><a href="javascript:void(0);">Villas</a></li>                      
                    </ul>
            </div>                                                                                  
            <div class="form-group hidden-xs left-btn">
                <input type="hidden" name="search-type" value="explore" />
                <input type="submit" id="searchPropertySubmit" class="btn button-search" value="<?php esc_attr_e('Search Now', 'reales'); ?>">
                <a href="javascript:void(0);" class="btn btn-o btn-white pull-right visible-xs" id="advanced"><?php esc_html_e('Advanced Search', 'reales'); ?> <span class="fa fa-angle-up"></span></a>
            </div>
        </form>

这是搜索页面的代码。

$form_type = isset($_GET['search-type']);
if($form_type == 'explore') { 
if( !function_exists('reales_search_properties') ): 
function reales_search_properties() {
    $search_id = isset($_GET['search_id']) ? sanitize_text_field($_GET['search_id']) : '';
    $search_name = isset($_GET['search_name']) ? sanitize_text_field($_GET['search_name']) : '';
    $search_country = isset($_GET['search_country']) ? sanitize_text_field($_GET['search_country']) : '';
    $search_state = isset($_GET['search_state']) ? sanitize_text_field($_GET['search_state']) : '';
    $search_city = isset($_GET['search_city']) ? sanitize_text_field($_GET['search_city']) : '';
    $search_category = isset($_GET['search_category']) ? sanitize_text_field($_GET['search_category']) : '';
    $search_type = isset($_GET['search_type']) ? sanitize_text_field($_GET['search_type']) : '';
    $search_min_price = isset($_GET['search_min_price']) ? sanitize_text_field($_GET['search_min_price']) : '';
    $search_max_price = isset($_GET['search_max_price']) ? sanitize_text_field($_GET['search_max_price']) : '';
    $search_bedrooms = isset($_GET['search_bedrooms']) ? sanitize_text_field($_GET['search_bedrooms']) : '';
    $search_bathrooms = isset($_GET['search_bathrooms']) ? sanitize_text_field($_GET['search_bathrooms']) : '';
    $search_neighborhood = isset($_GET['search_neighborhood']) ? sanitize_text_field($_GET['search_neighborhood']) : '';
    $search_p_cat = isset($_GET['search_p_cat']) ? sanitize_text_field($_GET['search_p_cat']) : '';
    $search_p_type = isset($_GET['search_p_type']) ? sanitize_text_field($_GET['search_p_type']) : '';
    $search_min_area = isset($_GET['search_min_area']) ? sanitize_text_field($_GET['search_min_area']) : '';
    $search_max_area = isset($_GET['search_max_area']) ? sanitize_text_field($_GET['search_max_area']) : '';
    $search_projects = isset($_GET['search_projects']) ? sanitize_text_field($_GET['search_projects']) : '';
    $reales_appearance_settings = get_option('reales_appearance_settings');
    $posts_per_page_setting = isset($reales_appearance_settings['reales_properties_per_page_field']) ? $reales_appearance_settings['reales_properties_per_page_field'] : '';
    $posts_per_page = $posts_per_page_setting != '' ? $posts_per_page_setting : 10;
    $sort = isset($_GET['sort']) ? sanitize_text_field($_GET['sort']) : 'newest';

    global $paged;

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array(
        'posts_per_page' => $posts_per_page,
        'paged' => $paged,
        'post_type' => 'property',
        'post_status' => 'publish',
        'property_category' => 'explore'
    );

    if($sort == 'newest') {
        $args['meta_key'] = 'property_featured';
        $args['orderby'] = array('meta_value_num' => 'DESC', 'date' => 'DESC');
    } else if($sort == 'price_lo') {
        $args['meta_key'] = 'property_price';
        $args['orderby'] = 'meta_value_num';
        $args['order'] = 'ASC';
    } else if($sort == 'price_hi') {
        $args['meta_key'] = 'property_price';
        $args['orderby'] = 'meta_value_num';
        $args['order'] = 'DESC';
    } else if($sort == 'bedrooms') {
        $args['meta_key'] = 'property_bedrooms';
        $args['orderby'] = 'meta_value_num';
        $args['order'] = 'DESC';
    } else if($sort == 'bathrooms') {
        $args['meta_key'] = 'property_bathrooms';
        $args['orderby'] = 'meta_value_num';
        $args['order'] = 'DESC';
    } else if($sort == 'area') {
        $args['meta_key'] = 'property_area';
        $args['orderby'] = 'meta_value_num';
        $args['order'] = 'DESC';
    }

    if($search_id != '') {
        $args['p'] = $search_id;
    }

    if($search_category != '0' && $search_type != '0') {
        $args['tax_query'] = array(
            'relation' => 'OR',
            array(
                'taxonomy' => 'property_category',
                'field'    => 'term_id',
                'terms'    => $search_category,
                'operator'  => 'IN'
            ),
            array(
                'taxonomy' => 'property_type_category',
                'field'    => 'term_id',
                'terms'    => $search_type,
                'operator'  => 'IN'
            ),
        );
    } else if($search_category != '0') {
        $args['tax_query'] = array(
            array(
                'taxonomy' => 'property_category',
                'field'    => 'term_id',
                'terms'    => $search_category,
                'operator'  => 'IN'
            ),
        );
    } else if($search_type != '0') {
        $args['tax_query'] = array(
            array(
                'taxonomy' => 'property_type_category',
                'field'    => 'term_id',
                'terms'    => $search_type,
                'operator'  => 'IN'
            ),
        );
    }

    $args['meta_query'] = array('relation' => 'OR');

    if($search_country != '') {
        array_push($args['meta_query'], array(
            'key'     => 'property_country',
            'value'   => $search_country,
        ));
    }

    if($search_state != '') {
        array_push($args['meta_query'], array(
            'key'     => 'property_state',
            'value'   => $search_state,
        ));
    }

    if($search_name != '') {
        array_push($args['meta_query'], array(
            'key'     => 'property_name',
            'value'   => $search_name,
            'compare' => '='
        ));
    }

    if($search_city != '') {
        array_push($args['meta_query'], array(
            'key'     => 'property_city',
            'value'   => $search_city,
        ));
    }

    if($search_min_price != '' && $search_min_price != '' && is_numeric($search_min_price) && is_numeric($search_max_price)) {
        array_push($args['meta_query'], array(
            'key'     => 'property_price',
            'value'   => array($search_min_price, $search_max_price),
            'compare' => 'BETWEEN',
            'type' => 'NUMERIC'
        ));
    } else if($search_min_price != '' && is_numeric($search_min_price)) {
        array_push($args['meta_query'], array(
            'key'     => 'property_price',
            'value'   => $search_min_price,
            'compare' => '>=',
            'type' => 'NUMERIC'
        ));
    } else if($search_max_price != '' && is_numeric($search_max_price)) {
        array_push($args['meta_query'], array(
            'key'     => 'property_price',
            'value'   => $search_max_price,
            'compare' => '<=',
            'type' => 'NUMERIC'
        ));
    }

    if($search_bedrooms != '' && $search_bedrooms != 0) {
        array_push($args['meta_query'], array(
            'key'     => 'property_bedrooms',
            'value'   => $search_bedrooms,
            'compare' => '='
            /*'type' => 'NUMERIC'*/
        ));
    }

    if($search_bathrooms != '' && $search_bathrooms != 0) {
        array_push($args['meta_query'], array(
            'key'     => 'property_bathrooms',
            'value'   => $search_bathrooms,
            'compare' => '>=',
            'type' => 'NUMERIC'
        ));
    }

    if($search_p_cat != '') {
        array_push($args['meta_query'], array(
            'key'     => 'property_cat',
            'value'   => $search_p_cat,
            'compare' => 'LIKE'
        ));
    }

    if($search_p_type != '') {
        array_push($args['meta_query'], array(
            'key'     => 'property_cat_type',
            'value'   => $search_p_type,
            'compare' => 'LIKE'
        ));
    }

    if($search_neighborhood != '') {
        array_push($args['meta_query'], array(
            'key'     => 'property_neighborhood',
            'value'   => $search_neighborhood,
            'compare' => 'LIKE'
        ));
    }

     if($search_projects != '') {
        array_push($args['meta_query'], array(
            'key'     => 'property_project',
            'value'   => $search_projects,
            'compare' => '='
        ));
    } 

    if($search_min_area != '' && $search_min_area != '' && is_numeric($search_min_area) && is_numeric($search_max_area)) {
        array_push($args['meta_query'], array(
            'key'     => 'property_area',
            'value'   => array($search_min_area, $search_max_area),
            'compare' => 'BETWEEN',
            'type' => 'NUMERIC'
        ));
    } else if($search_min_area != '' && is_numeric($search_min_area)) {
        array_push($args['meta_query'], array(
            'key'     => 'property_area',
            'value'   => $search_min_area,
            'compare' => '>=',
            'type' => 'NUMERIC'
        ));
    } else if($search_max_area != '' && is_numeric($search_max_area)) {
        array_push($args['meta_query'], array(
            'key'     => 'property_area',
            'value'   => $search_max_area,
            'compare' => '<=',
            'type' => 'NUMERIC'
        ));
    }

    $reales_amenities_settings = get_option('reales_amenities_settings');
    $amenities_list = array();
    $amenities = $reales_amenities_settings['reales_amenities_field'];
    $amenities_list = explode(',', $amenities);

    if($amenities != '') {
        foreach($amenities_list as $key => $value) {
            $post_var_name = str_replace(' ', '_', trim($value));

            $input_name = reales_substr45(sanitize_title($post_var_name));
            $input_name = sanitize_key($input_name);
            if (isset($_GET[$input_name]) && esc_html($_GET[$input_name]) == 1) {
                array_push($args['meta_query'], array(
                    'key'     => $input_name,
                    'value'   => 1
                ));
            }
        }
    }

    $query = new WP_Query($args);
    wp_reset_postdata();
    return $query;
}
endif;
if( !function_exists('reales_get_search_link') ): 
function reales_get_search_link() {
    $pages = get_pages(array(
        'meta_key' => '_wp_page_template',
        'meta_value' => 'property-search-results.php'
    ));

    if($pages) {
        foreach ($pages as $page) {
            $search_submit = get_permalink($page->ID);
        }
    } else {
        $search_submit = '';
    }

    return $search_submit;
}
endif;
}

0 个答案:

没有答案