Skip to content

Instantly share code, notes, and snippets.

@JoeHana
Last active July 29, 2018 10:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoeHana/6cd0dd9662a962235abeda27a0e5ea95 to your computer and use it in GitHub Desktop.
Save JoeHana/6cd0dd9662a962235abeda27a0e5ea95 to your computer and use it in GitHub Desktop.
Add Space (Living Space) Query to WPCasa Search
<?php
/**
* Add Space Fields to advanced search
*/
add_filter( 'wpsight_get_advanced_search_fields', 'custom_get_advanced_search_fields', 12 );
function custom_get_advanced_search_fields( $fields_advanced ) {
// Set advanced form fields
$fields_advanced['space_min'] = array(
'label' => __( 'Space (min)', 'wpcasa-advanced-search' ),
'key' => '_details_4',
'type' => 'text',
'data_compare' => '>=',
'data_type' => 'numeric',
'advanced' => true,
'class' => 'col-sm-12 col-sm-3',
'priority' => 105
);
$fields_advanced['space_max'] = array(
'label' => __( 'Space (max)', 'wpcasa-advanced-search' ),
'key' => '_details_4',
'type' => 'text',
'data_compare' => '<=',
'data_type' => 'numeric',
'advanced' => true,
'class' => 'col-sm-12 col-sm-3',
'priority' => 106
);
return $fields_advanced;
}
/**
* Add custom listing query vars
*/
add_filter( 'wpsight_listing_query_vars', 'custom_listing_query_vars', 11 );
function custom_listing_query_vars( $vars ) {
$vars['space_min'] = get_query_var( 'space_min' );
$vars['space_max'] = get_query_var( 'space_max' );
return $vars;
}
/**
* Extend the Listing Query to return listings based on a specifc detail
* This is used for Search by Space
*/
add_filter( 'wpsight_get_listings_query_args', 'custom_get_listings_query_args', 11, 2 );
function custom_get_listings_query_args( $query_args, $args ) {
if ( ! empty( $args['space_min'] ) ) {
$query_args['meta_query']['space_min'] = array(
'key' => '_details_4',
'value' => preg_replace( '/\D/', '', $args['space_min'] ),
'compare' => '>=',
'type' => 'numeric'
);
}
// Set meta query for max (maximum price)
if ( ! empty( $args['space_max'] ) ) {
$query_args['meta_query']['space_max'] = array(
'key' => '_details_4',
'value' => preg_replace( '/\D/', '', $args['space_max'] ),
'compare' => '<=',
'type' => 'numeric'
);
}
return $query_args;
}
/**
* Add custom query vars
*/
add_filter( 'query_vars', 'custom_query_vars_filter', 11 );
function custom_query_vars_filter( $vars ) {
$vars[] = "space_min";
$vars[] = "space_max";
return $vars;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment