function epl_search_pre_get_posts($query) { if (is_admin() || !$query->is_main_query()) { return; } if (epl_is_search()) { $paged = get_query_var('paged') ? get_query_var('paged') : 1; $query->init(); $query->set('posts_per_page', get_option('posts_per_page')); $query->set('paged', $paged); extract($_REQUEST); if (isset($property_id)) { if (is_numeric($property_id)) { } else { $query->set('epl_post_title', sanitize_text_field($property_id)); } } if (isset($property_agent)) { $property_agent = sanitize_title_with_dashes($property_agent); if ($property_agent = get_user_by('slug', $property_agent)) { $query->set('post_author', $property_agent->ID); } } if (isset($post_type) && !empty($post_type)) { $query->set('post_type', $post_type); } else { $epl_post_types = epl_get_active_post_types(); if (!empty($epl_post_types)) { $epl_post_types = array_keys($epl_post_types); $query->set('post_type', $epl_post_types); } } $epl_meta_query = array(); $epl_search_form_fields = epl_search_widget_fields_frontend($post_type, $property_status); foreach ($epl_search_form_fields as $epl_search_form_field) { if (isset($epl_search_form_field['query'])) { if ($epl_search_form_field['query']['query'] == 'meta') { $this_meta_query = array(); if (isset($epl_search_form_field['query']['multiple']) && $epl_search_form_field['query']['multiple'] == true) { if (isset(${$epl_search_form_field['meta_key']}) && !empty(${$epl_search_form_field['meta_key']})) { $this_meta_query['relation'] = isset($epl_search_form_field['query']['relation']) ? $epl_search_form_field['query']['relation'] : 'OR'; foreach ($epl_search_form_field['query']['sub_queries'] as $sub_query) { $this_sub_query = array('key' => $sub_query['key'], 'value' => ${$epl_search_form_field['meta_key']}, 'type' => $sub_query['type'], 'compare' => $sub_query['compare']); $this_meta_query[] = $this_sub_query; } $epl_meta_query[] = $this_meta_query; } } else { $query_meta_key = isset($epl_search_form_field['query']['key']) ? $epl_search_form_field['query']['key'] : $epl_search_form_field['meta_key']; if ($query_meta_key == 'property_unique_id' && isset(${$epl_search_form_field['meta_key']}) && !is_numeric(${$epl_search_form_field['meta_key']})) { continue; } if (isset(${$epl_search_form_field['meta_key']}) && !empty(${$epl_search_form_field['meta_key']})) { $this_meta_query = array('key' => $query_meta_key, 'value' => ${$epl_search_form_field['meta_key']}); isset($epl_search_form_field['query']['compare']) ? $this_meta_query['compare'] = $epl_search_form_field['query']['compare'] : ''; isset($epl_search_form_field['query']['type']) ? $this_meta_query['type'] = $epl_search_form_field['query']['type'] : ''; isset($epl_search_form_field['query']['value']) ? $this_meta_query['value'] = $epl_search_form_field['query']['value'] : ''; $epl_meta_query[] = $this_meta_query; } } } } } if (!empty($epl_meta_query)) { $query->set('meta_query', $epl_meta_query); } $tax_query = array(); if (isset($property_location) && !empty($property_location)) { $tax_query[] = array('taxonomy' => 'location', 'field' => 'id', 'terms' => $property_location); } if (!empty($tax_query)) { $query->set('tax_query', $tax_query); } $query->parse_query(); } }
/** * This shortcode allows for you to specify the property type(s) using * [listing_search title="" post_type="property" property_status="current/sold/leased" search_house_category="on/off" search_price="on/off" search_bed="on/off" search_bath="on/off" search_car="on/off" search_other="on/off"] option */ function epl_shortcode_listing_search_callback($atts) { global $wpdb; $atts = shortcode_atts(epl_search_get_defaults(), $atts); extract($atts); $selected_post_types = $atts['post_type']; extract($_GET); $queried_post_type = isset($_GET['post_type']) ? $_GET['post_type'] : ''; if (!is_array($selected_post_types)) { $selected_post_types = explode(",", $selected_post_types); $selected_post_types = array_map('trim', $selected_post_types); } global $epl_settings; ob_start(); $tabcounter = 1; if (!empty($selected_post_types)) { if (count($selected_post_types) > 1) { echo "<ul class='epl-search-tabs property_search-tabs epl-search-{$style}'>"; foreach ($selected_post_types as $post_type) { if (isset($_GET['action']) && $_GET['action'] == 'epl_search') { if ($queried_post_type == $post_type) { $is_sb_current = 'epl-sb-current'; } else { $is_sb_current = ''; } } else { $is_sb_current = $tabcounter == 1 ? 'epl-sb-current' : ''; } $post_type_label = isset($epl_settings['widget_label_' . $post_type]) ? $epl_settings['widget_label_' . $post_type] : $post_type; echo '<li data-tab="epl_ps_tab_' . $tabcounter . '" class="tab-link ' . $is_sb_current . '">' . $post_type_label . '</li>'; $tabcounter++; } echo '</ul>'; } ?> <div class="epl-search-forms-wrapper epl-search-<?php echo $style; ?> "> <?php $tabcounter = 1; // reset tab counter foreach ($selected_post_types as $post_type) { if (isset($_GET['action']) && $_GET['action'] == 'epl_search') { if ($queried_post_type == $post_type) { $is_sb_current = 'epl-sb-current'; } else { $is_sb_current = ''; } } else { $is_sb_current = $tabcounter == 1 ? 'epl-sb-current' : ''; } ?> </ul> <div class="epl-search-form <?php echo $is_sb_current; ?> " id="epl_ps_tab_<?php echo $tabcounter; ?> "> <?php if (isset($show_title) && $show_title == 'true') { if (!empty($title)) { ?> <h3><?php echo $title; ?> </h3><?php } } ?> <form method="get" action="<?php echo esc_url(home_url('/')); ?> "> <input type="hidden" name="action" value="epl_search" /> <?php $epl_frontend_fields = epl_search_widget_fields_frontend($post_type, $property_status); foreach ($epl_frontend_fields as $epl_frontend_field) { if ($epl_frontend_field['key'] == 'search_house_category' && isset($house_category_multiple) && $house_category_multiple == 'on') { $epl_frontend_field['multiple'] = true; $epl_frontend_field['query'] = array('query' => 'meta', 'compare' => 'IN'); } $config = isset(${$epl_frontend_field['key']}) ? ${$epl_frontend_field['key']} : ''; $value = isset(${$epl_frontend_field['meta_key']}) ? ${$epl_frontend_field['meta_key']} : ''; epl_widget_render_frontend_fields($epl_frontend_field, $config, $value, $post_type, $property_status); } ?> <div class="epl-search-submit-row epl-search-submit property-type-search"> <input type="submit" value="<?php echo $submit_label != '' ? $submit_label : __('Search', 'epl'); ?> " class="epl-search-btn" /> </div> </form> </div> <?php $tabcounter++; } } ?> </div> <?php return ob_get_clean(); }