function wpsight_options_search() { /** Create options array */ $options_search = array(); $options_search['heading_search'] = array('name' => _x('Search', 'options', 'wpsight'), 'id' => 'heading_search', 'type' => 'heading'); $listing_search_show = array('search' => __('Listing search results', 'wpsight'), 'archive' => __('Listing archive pages', 'wpsight'), 'templates' => __('Listing page templates', 'wpsight'), 'author' => __('Listing agent archive pages', 'wpsight')); $listing_search_show_defaults = array('search' => '1', 'archive' => '1', 'templates' => '1'); $options_search['search_show'] = array('name' => __('Show Search on', 'wpsight'), 'desc' => __('Please select where to display the listing search form. Keep in mind that there is also a widget to place the search form in widget areas.', 'wpsight'), 'id' => 'search_show', 'std' => $listing_search_show_defaults, 'type' => 'multicheck', 'options' => $listing_search_show); /** Loop through search form details */ $listing_search_options = array(); $listing_search_options_defaults = array(); foreach (wpsight_search_form_details() as $detail => $value) { // Check if advanced search field if ($value['advanced'] == true) { continue; } $listing_search_options[$detail] = $value['label']; $listing_search_options_defaults[$detail] = '1'; } $options_search['search_details'] = array('name' => __('Options', 'wpsight'), 'desc' => __('Please select the main search options to display in the search form.', 'wpsight'), 'id' => 'search_details', 'std' => $listing_search_options_defaults, 'type' => 'multicheck', 'options' => $listing_search_options); /** Toggle advanced search */ $options_search['search_advanced'] = array('name' => __('Advanced Search', 'wpsight'), 'desc' => __('Please check the box to enable advanced search.', 'wpsight'), 'id' => 'search_advanced', 'std' => '1', 'type' => 'checkbox'); /** Loop through search form details */ $listing_search_options = array(); $listing_search_options_defaults = array(); foreach (wpsight_search_form_details() as $detail => $value) { // Check if advanced search field if ($value['advanced'] == false) { continue; } $listing_search_options[$detail] = $value['label']; $listing_search_advanced_options_defaults[$detail] = '1'; } $options_search['search_advanced_options'] = array('name' => __('Advanced Options', 'wpsight'), 'desc' => __('Please select the advanced search options to display in the search form.', 'wpsight'), 'id' => 'search_advanced_options', 'std' => $listing_search_advanced_options_defaults, 'type' => 'multicheck', 'class' => 'hidden', 'options' => $listing_search_options); if (taxonomy_exists('feature')) { /** Toggle advanced filters */ $options_search['search_filters'] = array('name' => __('Advanced Filters', 'wpsight'), 'desc' => __('Please check the box to enable advanced filters.', 'wpsight'), 'id' => 'search_filters', 'std' => '0', 'class' => 'hidden', 'type' => 'checkbox'); /** Loop through feature terms */ $listing_search_filters = array(); foreach (get_terms('feature') as $feature) { $feature_name = $feature->term_id . ',' . $feature->name; $listing_search_filters[$feature_name] = $feature->name; } /** Add empty element */ array_unshift($listing_search_filters, ''); $filters_nr = apply_filters('wpsight_listing_search_filters_nr', 8); for ($i = 1; $i <= $filters_nr; $i++) { $options_search['filter_' . $i] = array('name' => __('Filter', 'wpsight') . ' #' . $i, 'desc' => __('Please select a feature.', 'wpsight'), 'id' => 'filter_' . $i, 'type' => 'select', 'class' => 'hidden', 'options' => $listing_search_filters); } } return apply_filters('wpsight_options_search', $options_search); }
function wpsight_get_listing_search() { // Get search form details $search_details = wpsight_get_option('search_details', true); // Check how many detail options are active $search_details_count = false; if (is_array($search_details)) { $search_details_tmp = array_filter($search_details); $search_details_count = count($search_details_tmp); } // Get advanced search options $advanced = wpsight_get_option('search_advanced'); $advanced_options = wpsight_get_option('search_advanced_options'); // Search form labels $search_labels = array('default' => __('Keyword or Listing ID', 'wpsight') . '…', 'submit' => __('Search', 'wpsight'), 'advanced' => __('Advanced Search', 'wpsight'), 'reset' => __('Reset', 'wpsight'), 'price' => __('Price', 'wpsight'), 'price_min' => __('Price (min)', 'wpsight'), 'price_max' => __('Price (max)', 'wpsight'), 'orderby' => __('Orderby', 'wpsight'), 'order' => __('Order', 'wpsight'), 'desc' => __('descending', 'wpsight'), 'asc' => __('ascending', 'wpsight'), 'results' => __('Results', 'wpsight')); $search_labels = apply_filters('wpsight_listing_search_labels', $search_labels); $listing_search = '<div class="listing-search count-' . $search_details_count . ' clearfix clear">'; /** * Get current query */ $search_get_query = false; // Get query cookie $search_cookie = apply_filters('wpsight_search_cookie', true); // Set query get $search_get = apply_filters('wpsight_search_get', true); if (!isset($_GET['s']) && isset($_COOKIE[WPSIGHT_COOKIE_SEARCH_QUERY]) && $search_cookie == true) { // When not search and cookie exists set last query $search_get_query = wpsight_explode_array(',', $_COOKIE[WPSIGHT_COOKIE_SEARCH_QUERY]); } elseif ($search_get == true) { foreach ($_GET as $get => $get_v) { $search_get_query[$get] = $get_v; } } $listing_search .= '<form method="get" action="' . apply_filters('wpsight_do_listing_search_form_action', home_url('/')) . '">'; $listing_search .= '<div class="form-inner">'; // Text field and search button $search_get_query['s'] = isset($search_get_query['s']) ? $search_get_query['s'] : false; $listing_search .= '<div class="listing-search-main">' . "\n"; $listing_search .= '<input type="text" class="listing-search-text text" name="s" title="' . $search_labels['default'] . '" value="' . $search_get_query['s'] . '" />' . "\n"; $listing_search .= '<input type="submit" class="' . apply_filters('wpsight_button_class_search', 'listing-search-submit btn btn-large btn-primary') . '" name="search-submit" value="' . $search_labels['submit'] . '" />' . "\n"; $listing_search .= '</div><!-- .listing-search-main -->' . "\n\n"; // Check if there are detail options if (!empty($search_details_tmp)) { // Details select drop downs $listing_search .= '<div class="listing-search-details">' . "\n"; // Loop through search form fields $listing_search .= wpsight_search_form_fields(wpsight_search_form_details(), $search_details, $search_get_query); $listing_search .= '</div><!-- .listing-search-details -->' . "\n\n"; } if ($advanced) { // Advanced Search $listing_search .= '<div class="listing-search-advanced ' . apply_filters('wpsight_listing_search_status', '') . '">' . "\n"; // Loop through search form fields (advanced) $listing_search .= wpsight_search_form_fields(wpsight_search_form_details(), $advanced_options, $search_get_query, true); // Add search form filters $listing_search .= wpsight_search_form_filters(wpsight_search_form_details(), $advanced_options, $search_get_query, true); $listing_search .= '</div><!-- .listing-search-advanced -->' . "\n\n"; // Advanced search button $advanced_search_button = '<div class="listing-search-advanced-button ' . apply_filters('wpsight_listing_search_status', '') . '">' . $search_labels['advanced'] . '</div>' . "\n"; } // Advanced search and reset button $listing_search .= '<div class="listing-search-buttons">' . "\n"; if (isset($_COOKIE[WPSIGHT_COOKIE_SEARCH_QUERY]) && ($search_cookie == true || $search_get == true)) { $listing_search .= '<div class="listing-search-reset-button">' . $search_labels['reset'] . '</div>' . "\n"; } if (isset($advanced_search_button)) { $listing_search .= $advanced_search_button; } $listing_search .= '</div><!-- .listing-search-buttons -->' . "\n\n"; // Close form tag $listing_search .= '</div><!-- form-inner -->'; $listing_search .= '</form>' . "\n"; // Close form wrap $listing_search .= '</div><!-- .listing-search -->' . "\n"; return apply_filters('wpsight_get_listing_search', $listing_search); }
function wpsight_listing_search_query_args() { // Get all form fields / gets foreach ($_GET as $name => $value) { $search_get[$name] = $value; } global $wp_query; $s_false = false; $tax_query = array(); $meta_query = array(); // Check all form details and set create queries foreach (wpsight_search_form_details() as $detail => $value) { if (empty($value['type']) || $detail == 'nr') { continue; } if ($value['type'] == 'taxonomy') { // Get custom taxonomy if (isset($search_get[$detail]) && $search_get[$detail]) { $tax_query[$detail] = array('taxonomy' => $value['taxonomy'], 'field' => 'slug', 'terms' => explode(',', $search_get[$detail])); } // Check if search term is taxonomy term if (wpsight_search_is_taxnomy_term($search_get['s'], $value['taxonomy'])) { $tax_query[$detail] = array('taxonomy' => $value['taxonomy'], 'field' => 'slug', 'terms' => sanitize_title($search_get['s'])); $s_false = true; $search_get['s'] = false; } } elseif (($value['type'] == 'select' || $value['type'] == 'radio' || $value['type'] == 'text') && $value['key'] != false) { $search_get[$detail] = isset($search_get[$detail]) ? $search_get[$detail] : false; if ($search_get[$detail]) { $meta_query[$detail] = array('key' => $value['key'], 'value' => $search_get[$detail], 'compare' => $value['data_compare'], 'type' => $value['data_type']); } } } // Check filter checkboxes $filters_nr = apply_filters('wpsight_listing_search_filters_nr', 8); $instance = isset($instance) ? $instance : false; for ($i = 1; $i <= $filters_nr; $i++) { if (!empty($search_get['f' . $i])) { $tax_query['f' . $i] = array('taxonomy' => 'feature', 'field' => 'slug', 'terms' => sanitize_title($search_get['f' . $i])); } } for ($i = 1; $i <= $filters_nr; $i++) { $search_get['f' . $i] = strip_tags($instance['filter' . $i]); } // Add price range to meta query if (!empty($search_get['min']) && is_numeric(preg_replace('/\\D/', '', $search_get['min']))) { $meta_query['min'] = array('key' => '_price', 'value' => preg_replace('/\\D/', '', $search_get['min']), 'compare' => '>=', 'type' => 'numeric'); } if (!empty($search_get['max']) && is_numeric(preg_replace('/\\D/', '', $search_get['max']))) { $meta_query['max'] = array('key' => '_price', 'value' => preg_replace('/\\D/', '', $search_get['max']), 'compare' => '<=', 'type' => 'numeric'); } // Set order and orderby if (isset($search_get['orderby']) && $search_get['orderby'] == 'price') { $orderby = 'meta_value_num'; } elseif (isset($search_get['orderby'])) { $orderby = $search_get['orderby']; } else { $orderby = false; } $order = isset($search_get['order']) ? $search_get['order'] : 'DESC'; // Set results per page number $posts_per_page = isset($search_get['nr']) ? $search_get['nr'] : 12; if (!empty($search_get)) { $args = array('post_type' => wpsight_listing_post_type(), 'posts_per_page' => $posts_per_page, 's' => $search_get['s'], 'paged' => get_query_var('paged'), 'orderby' => $orderby, 'order' => $order); // Add tax query to search args if (!empty($tax_query)) { $args = array_merge($args, array('tax_query' => $tax_query)); } // Add meta query to search args if (!empty($meta_query)) { $args = array_merge($args, array('meta_query' => $meta_query)); } // Add orderby price to search args if (isset($search_get['orderby']) && $search_get['orderby'] == 'price') { $orderby_args = array('meta_key' => '_price'); $args = array_merge($args, $orderby_args); } // Exclude sold and rented global $wpdb; $exclude_sold_rented = $wpdb->get_col($wpdb->prepare("\n\t\t\t SELECT DISTINCT post_id FROM {$wpdb->postmeta}\n\t\t\t WHERE meta_key = '%s'\n\t\t\t AND meta_value = '%s'\n\t\t\t", '_price_sold_rented', '1')); if (!empty($exclude_sold_rented) && apply_filters('wpsight_exclude_sold_rented', false) == true) { $args = array_merge($args, array('post__not_in' => $exclude_sold_rented)); } // Correct global query if ($s_false) { $wp_query->is_home = false; $wp_query->is_search = true; } // Fallback for old filter $args = apply_filters('wpsight_listing_search_args', $args); // Finally return $args return apply_filters('wpsight_listing_search_query_args', $args); } }