function wpsight_search_form_fields($search_fields, $search_details, $search_get, $search_advanced = false) { // Create form fields $listing_search = ''; foreach ($search_fields as $detail => $value) { // Check if advanced search field if ($search_advanced == true && $value['advanced'] != true) { continue; } if ($search_advanced == false && $value['advanced'] == true) { continue; } if (isset($search_details[$detail]) && $search_details[$detail]) { $listing_search .= '<div class="listing-search-field listing-search-field-' . $value['type'] . ' listing-search-field-' . $detail . '">'; // Create custom options (select) if ($value['type'] == 'select') { // tell our js part what's the default value to help make things consistent $default = isset($value['default']) && $value['default'] ? ' data-default="' . $value['default'] . '"' : ''; $listing_search .= '<select class="listing-search-' . $detail . ' select" name="' . $detail . '"' . $default . '>' . "\n"; // Empty option with label $listing_search .= '<option value="">' . $value['label'] . '</option>' . "\n"; foreach ($value['data'] as $k => $v) { $search_get[$detail] = isset($search_get[$detail]) ? $search_get[$detail] : false; if (!empty($k)) { $search_get[$detail] = isset($search_get[$detail]) && !empty($search_get[$detail]) ? $search_get[$detail] : $value['default']; $listing_search .= '<option value="' . $k . '"' . selected($k, sanitize_title($search_get[$detail]), false) . '>' . $v . '</option>' . "\n"; } } $listing_search .= '</select><!-- .listing-search-' . $detail . ' -->' . "\n"; // Create custom options (text) } elseif ($value['type'] == 'text') { $search_get[$detail] = isset($search_get[$detail]) ? $search_get[$detail] : false; $listing_search .= '<input class="listing-search-' . $detail . ' text" title="' . $value['label'] . '" name="' . $detail . '" type="text" value="' . $search_get[$detail] . '" />' . "\n"; } elseif ($value['type'] == 'number') { $search_get[$detail] = isset($search_get[$detail]) ? $search_get[$detail] : false; $listing_search .= '<input class="listing-search-' . $detail . ' text" title="' . $value['label'] . '" name="' . $detail . '" type="number" value="' . $search_get[$detail] . '" placeholder="' . $value['label'] . '"/>' . "\n"; } elseif ($value['type'] == 'radio') { // Create radio options if (!empty($value['label'])) { $listing_search .= '<label class="radiogroup" for="' . $detail . '">' . $value['label'] . '</label>' . "\n"; } $search_get[$detail] = isset($search_get[$detail]) ? $search_get[$detail] : $value['default']; foreach ($value['data'] as $k => $v) { $default = $value['default'] == $k ? 'true' : 'false'; $listing_search .= '<label class="radio"><input type="radio" name="' . $detail . '" value="' . $k . '"' . checked($k, $search_get[$detail], false) . ' data-default="' . $default . '"/> ' . $v . '</label>' . "\n"; } // Create taxonomy options } elseif ($value['type'] == 'taxonomy') { if (isset($value['data']['dropdown']) && $value['data']['dropdown'] === true) { $taxonomy = $value['taxonomy']; // Pre-select taxonomy if search is term $search_get[$taxonomy] = isset($search_get[$taxonomy]) ? $search_get[$taxonomy] : false; $taxonomy_s = wpsight_search_is_taxnomy_term($search_get['s'], $taxonomy) ? $search_get['s'] : $search_get[$taxonomy]; $taxonomy_s = !empty($taxonomy_s) ? sanitize_title($taxonomy_s) : false; // Set wp_dropdown_categories() $args $defaults = array('taxonomy' => $value['taxonomy'], 'show_option_none' => $value['label'], 'selected' => $taxonomy_s, 'hierarchical' => 1, 'echo' => 0, 'name' => $detail, 'class' => 'listing-search-' . $detail . ' select', 'walker' => new wpSight_Walker_TaxonomyDropdown(), 'orderby' => 'ID', 'order' => 'ASC', 'show_count' => 0, 'hide_empty' => 1, 'child_of' => 0, 'exclude' => '', 'id' => '', 'depth' => 0, 'hide_if_empty' => false, 'cache' => true); // Mix in search form fields $args $args = wp_parse_args($value['data'], $defaults); // Check if transients are active $transients = apply_filters('wpsight_transients_taxonomies', false, 'listings-search', $args, $value['taxonomy']); // If taxonomy transients are active if ($transients === true) { // If transient does not exist if (false !== $taxonomy_s || isset($value['data']['cache']) && $value['data']['cache'] === false) { // If cache is false, make sure to delete transient $transient = get_transient('wpsight_taxonomy_dropdown_' . $value['taxonomy']); if (false !== $transient) { delete_transient($transient); } // If an option is pre-selected, create dropdown dynamically $taxonomy_dropdown = wp_dropdown_categories($args); } else { // If we use default dropdown, use transient if (false === ($taxonomy_dropdown = get_transient('wpsight_taxonomy_dropdown_' . $value['taxonomy']))) { // Create taxonomy dropdown $taxonomy_dropdown = wp_dropdown_categories($args); // Set transient for this dropdown set_transient('wpsight_taxonomy_dropdown_' . $value['taxonomy'], $taxonomy_dropdown, DAY_IN_SECONDS); } } // If taxonomy transients are not active } else { if ($detail == 'property-type' || $detail == 'location') { //add custom param here //$args['search_type'] = $detail; //$args['show_option_none'] = ''; $taxonomy_dropdown = custom_multiple_dropdown_categories($args); } else { // Create taxonomy dropdown $taxonomy_dropdown = wp_dropdown_categories($args); } } // Add dropdown $listing_search .= $taxonomy_dropdown; } else { $listing_search .= '<select class="listing-search-' . $detail . ' select" name="' . $detail . '">' . "\n"; // Empty option with label $listing_search .= '<option value="">' . $value['label'] . '</option>' . "\n"; $taxonomy = $value['taxonomy']; // Pre-select taxonomy if search is term $search_get[$taxonomy] = isset($search_get[$taxonomy]) ? $search_get[$taxonomy] : false; $taxonomy_s = wpsight_search_is_taxnomy_term($search_get['s'], $taxonomy) ? $search_get['s'] : $search_get[$taxonomy]; foreach ($value['data'] as $k => $v) { if ($v->parent != 0) { continue; } $listing_search .= '<option value="' . $v->slug . '"' . selected($v->slug, sanitize_title($taxonomy_s), false) . '>' . $v->name . '</option>' . "\n"; $children_1 = get_terms($taxonomy, array('child_of' => $v->term_id, 'orderby' => 'name', 'order' => 'ASC', 'hide_empty' => true)); if ($children_1) { foreach ($children_1 as $child_1) { if ($child_1->parent != $v->term_id || !in_multiarray($child_1->term_id, $value['data'])) { continue; } $listing_search .= '<option value="' . $child_1->slug . '"' . selected($child_1->slug, sanitize_title($taxonomy_s), false) . '>' . str_repeat('-', 1) . ' ' . $child_1->name . '</option>' . "\n"; $children_2 = get_terms($taxonomy, array('child_of' => $child_1->term_id, 'orderby' => 'name', 'order' => 'ASC', 'hide_empty' => true)); if ($children_2) { foreach ($children_2 as $child_2) { if ($child_2->parent != $child_1->term_id || !in_multiarray($child_2->term_id, $value['data'])) { continue; } $listing_search .= '<option value="' . $child_2->slug . '"' . selected($child_2->slug, sanitize_title($taxonomy_s), false) . '>' . str_repeat('-', 2) . ' ' . $child_2->name . '</option>' . "\n"; $children_3 = get_terms($taxonomy, array('child_of' => $child_2->term_id, 'orderby' => 'name', 'order' => 'ASC', 'hide_empty' => true)); if ($children_3) { foreach ($children_3 as $child_3) { if ($child_3->parent != $child_2->term_id || !in_multiarray($child_3->term_id, $value['data'])) { continue; } $listing_search .= '<option value="' . $child_3->slug . '"' . selected($child_3->slug, sanitize_title($taxonomy_s), false) . '>' . str_repeat('-', 3) . ' ' . $child_3->name . '</option>' . "\n"; $children_4 = get_terms($taxonomy, array('child_of' => $child_3->term_id, 'orderby' => 'name', 'order' => 'ASC', 'hide_empty' => true)); if ($children_4) { foreach ($children_4 as $child_4) { if ($child_4->parent != $child_3->term_id || !in_multiarray($child_4->term_id, $value['data'])) { continue; } $listing_search .= '<option value="' . $child_4->slug . '"' . selected($child_4->slug, sanitize_title($taxonomy_s), false) . '>' . str_repeat('-', 4) . ' ' . $child_4->name . '</option>' . "\n"; } } // endif $children_4 } } // endif $children_3 } } // endif $children_2 } } // endif $children_1 } $listing_search .= '</select><!-- .listing-search-' . $detail . ' -->' . "\n"; } } $listing_search .= '</div><!-- .listing-search-field .listing-search-field-' . $detail . ' -->'; } } return $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); } }