Skip to content

Instantly share code, notes, and snippets.

@tbiinfotech
Last active September 6, 2018 15:23
Show Gist options
  • Save tbiinfotech/ca217d6d66110522aca2afacd30df230 to your computer and use it in GitHub Desktop.
Save tbiinfotech/ca217d6d66110522aca2afacd30df230 to your computer and use it in GitHub Desktop.
Keyword searcn-> /wp-content/themes/cxre/search-listings.php
<?php
/**
* Search Listings Template
*
* @package WP Pro Real Estate 7
* @subpackage Template
*/
global $ct_options;
$ct_home_adv_search_style = isset( $ct_options['ct_home_adv_search_style'] ) ? $ct_options['ct_home_adv_search_style'] : '';
$ct_disable_listing_search_results_adv_search = isset( $ct_options['ct_disable_listing_search_results_adv_search'] ) ? $ct_options['ct_disable_listing_search_results_adv_search'] : '';
$ct_search_results_layout = isset( $ct_options['ct_search_results_layout'] ) ? $ct_options['ct_search_results_layout'] : '';
$ct_search_results_listing_style = isset( $ct_options['ct_search_results_listing_style'] ) ? $ct_options['ct_search_results_listing_style'] : '';
$ct_header_listing_search = isset( $ct_options['ct_header_listing_search'] ) ? esc_html( $ct_options['ct_header_listing_search'] ) : '';
$ct_enable_front_end_login = isset( $ct_options['ct_enable_front_end_login'] ) ? esc_html( $ct_options['ct_enable_front_end_login'] ) : '';
$ct_listing_email_alerts_page_id = isset( $ct_options['ct_listing_email_alerts_page_id'] ) ? esc_attr( $ct_options['ct_listing_email_alerts_page_id'] ) : '';
/*-----------------------------------------------------------------------------------*/
/* Query multiple taxonomies */
/*-----------------------------------------------------------------------------------*/
$taxonomies_to_search = array(
'beds' => 'Bedrooms',
'baths' => 'Bathrooms',
'property_type' => 'Property Type',
'ct_status' => 'Status',
'state' => 'State',
'zipcode' => 'Zipcode',
'city' => 'City',
'country' => 'Country',
'county' => 'county',
'community' => 'Community',
'additional_features' => 'Additional Features',
//'furnished_unfurnished' => 'Furnished/Unfurnished'
);
$search_values = array();
$tax_query = array();
foreach($taxonomies_to_search as $t => $l) {
$var_name = 'ct_'. $t;
if (!empty($_GET[$var_name])) {
$search_values[$t] = $_GET[$var_name];
}
}
if(!empty($_GET['ct_additional_features'])) {
foreach($_GET['ct_additional_features'] as $t) {
$ct_additional_feature = str_replace('%5B0%5D', '+', $_GET['ct_additional_features']);
$search_values[$t] = $_GET[$var_name];
}
}
$search_values['post_type'] = 'listings';
$search_values['paged'] = ct_currentPage();
$search_num = $ct_options['ct_listing_search_num'];
$search_values['showposts'] = $search_num;
/*-----------------------------------------------------------------------------------*/
/* Additional Feature Search */
/*-----------------------------------------------------------------------------------*/
if (isset($_GET['ct_additional_features']) && !empty($_GET['ct_additional_features'])) {
if (is_array($_GET['ct_additional_features'])) {
$additional_features = $_GET['ct_additional_features'];
foreach ($additional_features as $feature):
$search_values['tax_query'] = array (
array(
'taxonomy' => 'additional_features',
'field' => 'slug',
'terms' => $feature,
'relation' => 'AND'
),
);
endforeach;
//var_dump($additional_features);
}
}
/*-----------------------------------------------------------------------------------*/
/* Exclude Ghost Status */
/*-----------------------------------------------------------------------------------*/
$search_values['tax_query'] = array (
array(
'taxonomy' => 'ct_status',
'field' => 'slug',
'terms' => 'ghost', // exclude media posts in the news-cat custom taxonomy
'operator' => 'NOT IN'
),
);
/*-----------------------------------------------------------------------------------*/
/* Keyword Search on Title and Content */
/*-----------------------------------------------------------------------------------*/
add_action( 'pre_get_posts', function( $q ) {
if($title = $q->get('_meta_or_title')) {
add_filter( 'get_meta_sql', function($sql) use ($title) {
global $wpdb;
// Only run once:
static $nr = 0;
if(0 != $nr++) return $sql;
// Modify WHERE part:
$sql['where'] = sprintf(
" AND ( %s OR %s ) ",
$wpdb->prepare("{$wpdb->posts}.post_title like '%%%s%%'", $title),
$wpdb->prepare("{$wpdb->posts}.post_content like '%%%s%%'", $title),
mb_substr( $sql['where'], 5, mb_strlen( $sql['where'] ) )
);
return $sql;
});
}
});
if (!empty($_GET['ct_keyword'])) {
$ct_keyword = $_GET['ct_keyword'];
$search_values['_meta_or_title'] = $ct_keyword;
/* $search_values['meta_query'] = array(
array(
'meta_key' => 'keywords',
'value' => $ct_keyword,
'compare' => 'LIKE'
)
);
*/
$post_keyword = $_REQUEST['ct_keyword'];
global $wpdb;
$posts_data = $wpdb->get_results ("SELECT * FROM ".$wpdb->prefix ."posts WHERE post_type= 'listings' AND post_status= 'publish' AND (post_content like '%" .$post_keyword. "%' OR post_title like '%".$post_keyword. "%') ORDER BY post_title" );
$post_meta_data = $wpdb->get_results ("SELECT * FROM ".$wpdb->prefix ."posts WHERE ".$wpdb->prefix ."posts.post_status ='publish' AND ".$wpdb->prefix ."posts.post_type= 'listings' AND ".$wpdb->prefix ."posts.ID = (SELECT ".$wpdb->prefix ."postmeta.post_id FROM ".$wpdb->prefix ."postmeta WHERE ".$wpdb->prefix ."postmeta.meta_key = '_ct_listing_alt_title' AND ".$wpdb->prefix ."postmeta.meta_value LIKE '%".$post_keyword. "%' OR ".$wpdb->prefix ."postmeta.meta_key = '_ct_rental_title' AND ".$wpdb->prefix ."postmeta.meta_value LIKE '%".$post_keyword. "%' )");
$post_terms = get_posts(array(
'showposts' => -1,
'post_type' => 'listings',
'post_status' => 'publish',
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'community',
'field' => 'name',
'compare' => 'LIKE',
'terms' => array($post_keyword)
),
array(
'taxonomy' => 'city',
'field' => 'name',
'compare' => 'LIKE',
'terms' => array($post_keyword)
),
array(
'taxonomy' => 'zipcode',
'field' => 'name',
'compare' => 'LIKE',
'terms' => array($post_keyword)
),
array(
'taxonomy' => 'country',
'field' => 'name',
'compare' => 'LIKE',
'terms' => array($post_keyword)
),
array(
'taxonomy' => 'state',
'field' => 'name',
'compare' => 'LIKE',
'terms' => array($post_keyword)
)
))
);
$id_array_post = array();
foreach($posts_data as $post_terms_id)
{
array_push($id_array_post,$post_terms_id->ID);
};
$id_array_post_meta = array();
foreach($post_meta_data as $post_terms_id)
{
array_push($id_array_post_meta,$post_terms_id->ID);
};
$id_array = array();
foreach($post_terms as $post_terms_id)
{
array_push($id_array,$post_terms_id->ID);
};
$ids = array_unique(array_merge($id_array_post,$id_array_post_meta,$id_array));
$search_values = array(
'post_type' => array( 'listings' ),
'post__in' => $ids
);
}
/*-----------------------------------------------------------------------------------*/
/* Order by Price */
/*-----------------------------------------------------------------------------------*/
if (!empty($_GET['ct_orderby_price'])) {
$order = utf8_encode($_GET['ct_orderby_price']);
$search_values['orderby'] = 'meta_value';
$search_values['meta_key'] = '_ct_price';
$search_values['meta_type'] = 'numeric';
$search_values['order'] = $order;
}
/*-----------------------------------------------------------------------------------*/
/* Order by (Title, Price or upload date) */
/*-----------------------------------------------------------------------------------*/
if (!empty($_GET['ct_orderby'])) {
$orderBy = $_GET['ct_orderby'];
if ($orderBy == 'priceASC') {
$search_values['orderby'] = 'meta_value';
$search_values['meta_key'] = '_ct_price';
$search_values['meta_type'] = 'numeric';
$search_values['order'] = 'ASC';
} elseif ($orderBy == 'priceDESC') {
$search_values['orderby'] = 'meta_value';
$search_values['meta_key'] = '_ct_price';
$search_values['meta_type'] = 'numeric';
$search_values['order'] = 'DESC';
} elseif ($orderBy == 'dateDESC') {
$search_values['orderby'] = 'date';
$search_values['order'] = 'DESC';
}elseif ($orderBy == 'dateASC') {
$search_values['orderby'] = 'date';
$search_values['order'] = 'ASC';
} else { // titleASC
$search_values['orderby'] = 'title';
$search_values['order'] = 'ASC';
}
}
$mode = 'search';
/*-----------------------------------------------------------------------------------*/
/* Check Price From/To */
/*-----------------------------------------------------------------------------------*/
if (!empty($_GET['ct_price_from']) && !empty($_GET['ct_price_to'])) {
$ct_price_from = str_replace(',', '', $_GET['ct_price_from']);
$ct_price_to = str_replace(',', '', $_GET['ct_price_to']);
$search_values['meta_query'] = array(
array(
'key' => '_ct_price',
'value' => array( $ct_price_from, $ct_price_to ),
'type' => 'NUMERIC',
'compare' => 'BETWEEN'
)
);
}
else if (!empty($_GET['ct_price_from'])) {
$ct_price_from = str_replace(',', '', $_GET['ct_price_from']);
$search_values['meta_query'] = array(
array(
'key' => '_ct_price',
'value' => $ct_price_from,
'type' => 'NUMERIC',
'compare' => '>='
)
);
}
else if (!empty($_GET['ct_price_to'])) {
$ct_price_to = str_replace(',', '', $_GET['ct_price_to']);
$search_values['meta_query'] = array(
array(
'key' => '_ct_price',
'value' => $_GET['ct_price_to'],
'type' => 'NUMERIC',
'compare' => '<='
)
);
}
/*-----------------------------------------------------------------------------------*/
/* Check Dwelling Size From/To */
/*-----------------------------------------------------------------------------------*/
if (!empty($_GET['ct_sqft_from']) && !empty($_GET['ct_sqft_to'])) {
$ct_sqft_from = str_replace(',', '', $_GET['ct_sqft_from']);
$ct_sqft_to = str_replace(',', '', $_GET['ct_sqft_to']);
$search_values['meta_query'] = array(
array(
'key' => '_ct_sqft',
'value' => array( $ct_sqft_from, $ct_sqft_to ),
'type' => 'NUMERIC',
'compare' => 'BETWEEN'
)
);
}
else if (!empty($_GET['ct_sqft_from'])) {
$ct_sqft_from = str_replace(',', '', $_GET['ct_sqft_from']);
$search_values['meta_query'] = array(
array(
'key' => '_ct_sqft',
'value' => $ct_sqft_from,
'type' => 'NUMERIC',
'compare' => '>='
)
);
}
else if (!empty($_GET['ct_sqft_to'])) {
$ct_sqft_to = str_replace(',', '', $_GET['ct_sqft_to']);
$search_values['meta_query'] = array(
array(
'key' => '_ct_sqft',
'value' => $ct_sqft_to,
'type' => 'NUMERIC',
'compare' => '<='
)
);
}
/*-----------------------------------------------------------------------------------*/
/* Check Lot Size From/To */
/*-----------------------------------------------------------------------------------*/
if (!empty($_GET['ct_lotsize_from']) && !empty($_GET['ct_sqft_to'])) {
$ct_lotsize_from = str_replace(',', '', $_GET['ct_lotsize_from']);
$ct_lotsize_to = str_replace(',', '', $_GET['ct_lotsize_to']);
$search_values['meta_query'] = array(
array(
'key' => '_ct_lotsize',
'value' => array( $ct_sqft_from, $ct_sqft_to ),
'type' => 'NUMERIC',
'compare' => 'BETWEEN'
)
);
}
else if (!empty($_GET['ct_lotsize_from'])) {
$ct_lotsize_from = str_replace(',', '', $_GET['ct_lotsize_from']);
$search_values['meta_query'] = array(
array(
'key' => '_ct_lotsize',
'value' => $ct_lotsize_from,
'type' => 'NUMERIC',
'compare' => '>='
)
);
}
else if (!empty($_GET['ct_lotsize_to'])) {
$ct_lotsize_to = str_replace(',', '', $_GET['ct_lotsize_to']);
$search_values['meta_query'] = array(
array(
'key' => '_ct_lotsize',
'value' => $ct_lotsize_to,
'type' => 'NUMERIC',
'compare' => '<='
)
);
}
/*-----------------------------------------------------------------------------------*/
/* Check if pet friendly */
/*-----------------------------------------------------------------------------------*/
if (!empty($_GET['pet_friendly'])) {
$pet_friendly = $_GET['pet_friendly'];
$search_values['meta_query'] = array(
array(
'key' => 'pet_friendly',
'value' => $pet_friendly,
'type' => 'char',
'compare' => '='
)
);
}
/*-----------------------------------------------------------------------------------*/
/* Check to see if reference number matches */
/*-----------------------------------------------------------------------------------*/
if (!empty($_GET['ct_mls'])) {
$ct_mls = $_GET['ct_mls'];
$search_values['meta_query'] = array(
array(
'key' => '_ct_mls',
'value' => $ct_mls,
'type' => 'char',
'compare' => '='
)
);
}
/*-----------------------------------------------------------------------------------*/
/* Check to see if max number of employees matches */
/*-----------------------------------------------------------------------------------*/
if (!empty($_GET['ct_max_employees'])) {
$ct_max_employees = $_GET['ct_max_employees'];
$search_values['meta_query'] = array(
array(
'key' => 'number_of_employees',
'value' => $ct_max_employees,
'type' => 'NUMERIC',
'compare' => '>='
)
);
}
/*-----------------------------------------------------------------------------------*/
/* Save the existing query */
/*-----------------------------------------------------------------------------------*/
global $wp_query;
$existing_query_obj = $wp_query;
$wp_query = new WP_Query( $search_values );
$total_results = $wp_query->found_posts;
unset($search_values['post_type']);
unset($search_values['paged']);
unset($search_values['showposts']);
/*-----------------------------------------------------------------------------------*/
/* Prepare the title string by looping through all
/* the values we're going to query and put them together
/*-----------------------------------------------------------------------------------*/
$search_params = array();
$loop = 0;
foreach ($search_values as $t => $s) {
$term = get_term_by('slug',(int)$s,$t);
if($term != '0') {
$search_params[] = $term->name;
}
}
$search_params[] = isset( $_GET['ct_keyword'] ) ? $_GET['ct_keyword'] : '';
$search_params = implode(', ', $search_params);
get_header();
do_action('before_listings_search_header');
if($ct_header_listing_search != 'yes') {
echo '<!-- Title Header -->';
echo '<header id="title-header" class="marB0">';
echo '<div class="container">';
echo '<h5 class="marT0 marB8 left">';
echo esc_html($total_results);
echo ' ';
if($total_results != '1') { esc_html_e('listings found', 'contempo'); } else { esc_html_e('listing found', 'contempo'); }
echo '</h5>';
echo '<div class="muted right">';
//esc_html_e('Find A Home', 'contempo');
echo '</div>';
echo '<div class="clear"></div>';
echo '</div>';
echo '</header>';
echo '<!-- //Title Header -->';
}
if($ct_disable_listing_search_results_adv_search != 'no' && $ct_search_results_layout == 'sidebyside') {
echo '<!-- Searching On -->';
echo '<div class="side-by-side searching-on ' . $ct_home_adv_search_style . '">';
echo '<div class="container">';
echo '<span class="searching">' . __('Searching:', 'contempo') . '</span>';
if($search_params != "") {
echo '<span class="search-params">' . $search_params . '</span>';
} else {
echo '<span class="search-params">' . __('All listings', 'contempo') . '</span>';
}
echo '<span class="search-toggle"><span id="text-toggle">' . __('Open Search', 'contempo') . '</span><i class="fa fa-plus-square-o"></i></span>';
echo '</div>';
echo '</div>';
echo '<!-- //Searching On -->';
do_action('before_listings_adv_search');
echo '<section class="side-by-side search-results advanced-search ' . $ct_home_adv_search_style . '">';
echo '<div class="container">';
get_template_part('/includes/advanced-search');
echo '</div>';
echo'</section>';
echo '<div class="clear"></div>';
}
do_action('before_listings_search_map');
// Start Search Results Map
$wp_query = new wp_query( $search_values );
if($ct_options['ct_disable_google_maps_search'] == 'no') {
if($wp_query->have_posts() || $search_params == '' || $ct_header_listing_search == 'yes' ) {
echo '<!-- Map -->';
if($ct_search_results_layout == 'sidebyside') {
echo '<div id="map-wrap" class="listings-results-map col span_6 side-map">';
} else {
echo '<div id="map-wrap" class="listings-results-map">';
}
$search_values['post_type'] = 'listings';
$search_values['paged'] = ct_currentPage();
$search_values['showposts'] = $search_num;
$wp_query = new wp_query( $search_values );
if($wp_query->have_posts()) {
// Marker Navigation
ct_search_results_map_navigation();
// Map
ct_search_results_map();
}
// End Search Results Map
echo '</div>';
echo '<!-- //Map -->';
}
}
if($ct_header_listing_search == 'yes' && $ct_search_results_layout != 'sidebyside') {
echo '<!-- Title Header -->';
echo '<header id="title-header" class="marT0 marB0">';
echo '<div class="container">';
echo '<h5 class="marT0 marB8 left">';
echo esc_html($total_results);
echo ' ';
if($total_results != '1') { esc_html_e('listings found', 'contempo'); } else { esc_html_e('listing found', 'contempo'); }
echo '</h5>';
echo '<div class="muted right">';
//esc_html_e('Find A Home', 'contempo');
echo '</div>';
echo '<div class="clear"></div>';
echo '</div>';
echo '</header>';
echo '<!-- //Title Header -->';
if($ct_search_results_layout != 'sidebyside') {
echo '<!-- Searching On -->';
echo '<div class="searching-on ' . $ct_home_adv_search_style . '">';
echo '<div class="container">';
echo '<span class="searching">' . __('Searching:', 'contempo') . '</span>';
if($search_params != "") {
echo '<span class="search-params">' . $search_params . '</span>';
} else {
echo '<span class="search-params">' . __('All listings', 'contempo') . '</span>';
}
if($ct_options['ct_disable_google_maps_search'] == 'no') {
echo '<span class="map-toggle"><span id="text-toggle">' . __('Close Map', 'contempo') . '</span><i class="fa fa-minus-square-o"></i></span>';
}
echo '</div>';
echo '</div>';
echo '<!-- //Searching On -->';
}
}
do_action('before_listings_searching_on');
echo '<!-- Search Results -->';
if($ct_search_results_layout == 'sidebyside') {
echo '<div class="col span_6 side-results">';
}
if($ct_disable_listing_search_results_adv_search == 'no' && $ct_search_results_layout != 'sidebyside') {
echo '<!-- Searching On -->';
echo '<div class="searching-on ' . $ct_home_adv_search_style . '">';
echo '<div class="container">';
echo '<span class="searching">' . __('Searching:', 'contempo') . '</span>';
if($search_params != "") {
echo '<span class="search-params">' . $search_params . '</span>';
} else {
echo '<span class="search-params">' . __('All listings', 'contempo') . '</span>';
}
echo '<span class="map-toggle"><span id="text-toggle">' . __('Close Map', 'contempo') . '</span><i class="fa fa-minus-square-o"></i></span>';
echo '</div>';
echo '</div>';
echo '<!-- //Searching On -->';
do_action('before_listings_adv_search');
if($ct_disable_listing_search_results_adv_search == 'no') {
echo '<section class="search-results advanced-search ' . $ct_home_adv_search_style . '">';
echo '<div class="container">';
get_template_part('/includes/advanced-search');
echo '</div>';
echo'</section>';
echo '<div class="clear"></div>';
}
}
?>
<?php do_action('before_listing_search_results'); ?>
<div class="container">
<!-- Listing Results -->
<div id="listings-results" class="listing-search-results col span_12 first">
<div class="col span_12 <?php if($ct_disable_listing_search_results_adv_search == 'yes' && $ct_search_results_layout != 'sidebyside') { echo 'marT30'; } ?>">
<?php if (function_exists('ctea_show_alert_creation')) {
echo '<div class="col span_9 first">';
if(is_user_logged_in()) { ?>
<form method="post" action="" class="form-searched-save-search left">
<input type="hidden" name="search_args" value='<?php print base64_encode( serialize( $search_values ) ); ?>'>
<input type="hidden" name="search_URI" value="<?php echo $_SERVER['REQUEST_URI'] ?>">
<input type="hidden" name="action" value='ct_searched_save_search'>
<input type="hidden" name="ct_searched_save_search_ajax" value="<?php echo wp_create_nonce('ct-searched-save-search-nounce')?>">
<a id="searched-save-search" class="btn save-btn"><?php _e('Save This Search?', 'contempo'); ?></a>
</form>
<a id="view-saved" class="btn" href="<?php echo get_page_link($ct_listing_email_alerts_page_id); ?>"><?php _e('View Saved', 'contempo'); ?></a>
<?php } elseif($ct_enable_front_end_login != 'no') { ?>
<a id="searched-save-search" class="btn login-register save-btn"><?php _e('Save This Search?', 'contempo'); ?></a>
<?php }
echo '</div>';
} ?>
<div class="col <?php if (function_exists('ctea_show_alert_creation') && $ct_enable_front_end_login != 'no') { echo 'span_3'; } else { echo 'span_12 first'; } ?>">
<?php ct_sort_by(); ?>
</div>
</div>
<?php
// Reset Query for Listings
wp_reset_query();
wp_reset_postdata();
$search_values['post_type'] = 'listings';
$search_values['paged'] = ct_currentPage();
$search_values['showposts'] = $search_num;
$wp_query = new wp_query( $search_values );
if($ct_search_results_listing_style == 'list') {
get_template_part( 'layouts/list');
} else {
get_template_part( 'layouts/grid');
}
// End Listing Results
echo '<div class="clear"></div>';
echo '</div>';
echo '<!-- Listing Results -->';
// Restore WP_Query object
$wp_query = $existing_query_obj;
echo '<div class="clear"></div>';
echo '</div>';
if($ct_search_results_layout == 'sidebyside') {
echo '</div>';
}
echo '<!-- //Search Results -->';
do_action('after_listing_search_results');
get_footer(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment