Support » Plugins » I'm stuck with a search filter (dynamic)

  • noah2003

    (@noah2003)


    Hi guys,

    This is my first wordpress site and i am stuck with the php code.

    Here is the problem. I installed ACF and created a taxonomy with the state and city. So if i submit a listing and select a state you get to see only the cities in that state. So far so good.

    On the main site i have a advanced search bar with the different select fields in them. So far so good.

    The problem is i want to have the same option on the front end. So if somebody chooses a state they should only see the city from that state. I do not know what sort of code i should use for that. I guess it should be something like.. if state x then city x and then the options below.

    I hope that somebody can help me with this. Below are 2 php files which are used for the search bar.

    Advanced search.php

    <?php

    function query_set_only_author( $wp_query ) {
    global $current_user;
    if ( is_admin() && !current_user_can(‘manage_options’) ) {
    $wp_query->set( ‘author’, $current_user->ID );
    }
    }
    add_action(‘pre_get_posts’, ‘query_set_only_author’ );

    global $ct_options;
    $layout = $ct_options[‘ct_homepage_adv_search’][‘enabled’];

    ?>

    <h3 class=”marT0 marB0″><?php _e(‘Find your new home’, ‘contempo’); ?></h3>
    <p class=”marB0″><?php $count_posts = wp_count_posts(‘listings’); $published_listings = $count_posts->publish; ?><?php echo $published_listings ?> <?php _e(‘Real Estate Listings, Homes for Sale & Rental Properties.’, ‘contempo’); ?></p>

    <form id=”advanced_search” name=”search-listings” action=”<?php echo home_url(); ?>”>

    <?php

    if ($layout) :

    foreach ($layout as $key=>$value) {

    switch($key) {

    // City
    case ‘city’ : ?>
    <div class=”left”>
    <label for=”ct_city”><?php _e(‘City’, ‘contempo’); ?></label>
    <?php ct_search_form_select(‘city’); ?>
    </div>
    <?php
    break;

    // State
    case ‘state’ : ?>
    <div class=”left”>
    <label for=”ct_state”><?php _e(‘State’, ‘contempo’); ?></label>
    <?php ct_search_form_select(‘state’); ?>
    </div>
    <?php
    break;

    // Zipcode
    case ‘zipcode’ : ?>
    <div class=”left”>
    <label for=”ct_zipcode”><?php _e(‘Zipcode’, ‘contempo’); ?></label>
    <?php ct_search_form_select(‘zipcode’); ?>
    </div>
    <?php
    break;

    // Beds
    case ‘beds’ : ?>
    <div class=”left”>
    <label for=”ct_beds”><?php _e(‘Slaapkamers’, ‘contempo’); ?></label>
    <?php ct_search_form_select(‘field_532ab786c3410’); ?>
    </div>
    <?php
    break;

    // Baths
    case ‘baths’ : ?>
    <div class=”left”>
    <label for=”ct_baths”><?php _e(‘Baths’, ‘contempo’); ?></label>
    <?php ct_search_form_select(‘baths’); ?>
    </div>
    <?php
    break;

    // Status
    case ‘status’ : ?>
    <div class=”left”>
    <label for=”ct_status”><?php _e(‘Status’, ‘contempo’); ?></label>
    <?php ct_search_form_select(‘ct_status’); ?>
    </div>
    <?php
    break;

    // Additional Features
    case ‘additional_features’ : ?>
    <div class=”left”>
    <label for=”ct_additional_features”><?php _e(‘Addtional Features’, ‘contempo’); ?></label>
    <?php ct_search_form_select(‘additional_features’); ?>
    </div>
    <?php
    break;

    // Price From
    case ‘price_from’ : ?>
    <div class=”left”>
    <label for=”ct_price_from”><?php _e(‘Price From’, ‘contempo’); ?> (<?php currency(); ?>)</label>
    <input type=”text” id=”ct_price_from” class=”number” name=”ct_price_from” size=”8″ />
    </div>
    <?php
    break;

    // Price To
    case ‘price_to’ : ?>
    <div class=”left”>
    <label for=”ct_price_to”><?php _e(‘Price To’, ‘contempo’); ?> (<?php currency(); ?>)</label>
    <input type=”text” id=”ct_price_to” class=”number” name=”ct_price_to” size=”8″ />
    </div>
    <?php
    break;

    // MLS
    case ‘mls’ : ?>
    <div class=”left”>
    <label for=”ct_mls”><?php _e(‘MLS #’, ‘contempo’); ?></label>
    <input type=”text” id=”ct_mls” name=”ct_mls” size=”12″ />
    </div>
    <?php
    break;

    }

    }

    ?>

    <div class=”left”>
    <label for=”provincie”><?php _e(‘State’, ‘contempo’); ?></label>
    <select name=”provincie”>
    <option value=”>Maak een keuze</option>
    <option value=’drenthe’>Drenthe</option>
    <option value=’flevoland’>Flevoland</option>
    <option value=’friesland’>Friesland</option>
    <option value=’gelderland’>Gelderland</option>
    <option value=’groningen’>Groningen</option>
    <option value=’limburg’>Limburg</option>
    <option value=’noord-brabant’>Noord Brabant</option>
    <option value=’overijssel’>Overijssel</option>
    <option value=’utrecht’>Utrecht</option>
    <option value=’zeeland’>Zeeland</option>
    <option value=’zuid-holland’>Zuid Holland</option>
    </select>
    </div>

    <?php
    ?>

    <div class=”left”>
    <label for=”city”><?php _e(‘City’, ‘contempo’); ?></label>
    <select name=”woonplaatsen_drenthe”>
    <option value=”>Maak een keuze</option>
    <option value=’alteveer’>Alteveer</option>
    <option value=’flevoland’>Flevoland</option>
    <option value=’friesland’>Friesland</option>
    <option value=’gelderland’>Gelderland</option>
    <option value=’groningen’>Groningen</option>
    <option value=’limburg’>Limburg</option>
    <option value=’noord-brabant’>Noord Brabant</option>
    <option value=’overijssel’>Overijssel</option>
    <option value=’utrecht’>Utrecht</option>
    <option value=’zeeland’>Zeeland</option>
    <option value=’zuid-holland’>Zuid Holland</option>
    </select>
    </div>

    <?php

    ?>

    <div class=”left”>
    <label for=”type_woning”><?php _e(‘Type Woning’, ‘contempo’); ?></label>
    <select name=”type_woning”>
    <option value=”>Maak een keuze</option>
    <option value=’eengezinswoning’>Eengezinswoning</option>
    <option value=’grachtenpand’>Grachtenpand</option>
    <option value=’herenhuis’>Herenhuis</option>
    <option value=’tussenwoning’>Tussenwoning</option>
    <option value=’woning’>Woning</option>
    <option value=’twee-onder-een-kap’>Twee onder één kap</option>
    <option value=’hoekwoning’>Hoekwoning</option>
    <option value=’vrijstaande_woning’>Vrijstaande woning</option>
    <option value=’bungalow’>Bungalow</option>
    <option value=’landhuis’>Landhuis</option>
    <option value=’villa’>Villa</option>
    <option value=’woonboerderij’>Woonboerderij</option>
    <option value=’appartement’>Appartement</option>
    <option value=’kamer’>Kamer</option>
    <option value=’penthouse’>Penthouse</option>
    <option value=’serviceflat’>Serviceflat</option>
    <option value=’studio’>Studio</option>
    <option value=’recreatiewoning’>Recreatiewoning</option>
    </select>
    </div>

    <?php endif; ?>

    <input type=”hidden” name=”search-listings” value=”true” />
    <input id=”submit” class=”btn left” type=”submit” value=”<?php _e(‘Search’, ‘contempo’); ?>” />

    <div class=”clear”></div>

    </form>

    2. Search listing.php

    <?php

    global $ct_options;
    $layout = $ct_options[‘ct_layout’];

    /*———————————————————————————–*/
    /* Query multiple taxonomies */
    /*———————————————————————————–*/

    $taxonomies_to_search = array(
    ‘beds’ => ‘Beds’,
    ‘baths’ => ‘Bathrooms’,
    ‘property_type’ => ‘Property Type’,
    ‘ct_status’ => ‘Status’,
    ‘state’ => ‘State’,
    ‘zipcode’ => ‘Zipcode’,
    ‘city’ => ‘City’,
    ‘additional_features’ => ‘Additional Features’
    );

    $search_values = array();

    foreach ($taxonomies_to_search as $t => $l) {
    $var_name = ‘ct_’. $t;

    if (!empty($_GET[$var_name])) {
    $search_values[$t] = utf8_encode($_GET[$var_name]);
    }
    }

    $search_values[‘post_type’] = ‘listings’;
    $search_values[‘paged’] = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;

    $search_num = $ct_options[‘ct_listing_search_num’];
    $search_values[‘showposts’] = $search_num;

    $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’ => $ct_price_to,
    ‘type’ => ‘numeric’,
    ‘compare’ => ‘<=’
    )
    );
    }

    if(!empty($_GET[‘type_woning’]))
    $search_values[‘meta_query’][]=array(
    ‘key’ => ‘type_woning’,
    ‘compare’ => ‘==’,
    ‘value’ => $_GET[‘type_woning’],
    );

    if(!empty($_GET[‘kamers’]))
    $search_values[‘meta_query’][]=array(
    ‘key’ => ‘kamers’,
    ‘compare’ => ‘==’,
    ‘value’ => $_GET[‘kamers’],
    );

    if(!empty($_GET[‘provincie’]))
    $search_values[‘meta_query’][]=array(
    ‘key’ => ‘provincie’,
    ‘compare’ => ‘==’,
    ‘value’ => $_GET[‘provincie’],
    );

    if(!empty($_GET[‘woonplaatsen_drenthe’]))
    $search_values[‘meta_query’][]=array(
    ‘key’ => ‘woonplaatsen_drenthe’,
    ‘compare’ => ‘==’,
    ‘value’ => $_GET[‘woonplaatsen_drenthe’],
    );
    /*———————————————————————————–*/
    /* 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’ => ‘numeric’,
    ‘compare’ => ‘=’
    )
    );
    }

    global $wp_query;

    /*———————————————————————————–*/
    /* Save the existing 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 = ”;
    $loop = 0;
    foreach ($search_values as $t => $s) {
    if ($loop == 1) {
    $search_params .= ‘, ‘;
    } else {
    $loop = 1;
    }

    $term = get_term_by(‘slug’,$s,$t);
    $name = $term->name;

    $search_params .= $name;
    }

    get_header();

    echo ‘<header id=”archive-header” class=”marB40″>’;
    echo ‘<div class=”container”>’;
    echo ‘<h2 class=”marT0 marB0 left”>’;
    echo $total_results;
    echo ‘ ‘;
    if($total_results != ‘1’) { _e(‘listings found’, ‘contempo’); } else { _e(‘listing found’, ‘contempo’); }
    echo ‘</h2>’;
    echo ct_breadcrumbs();
    echo ‘<div class=”clear”></div>’;
    echo ‘</div>’;
    echo ‘</header>’;

    echo ‘<div class=”container”>’;

    echo ‘<div class=”searching-on”>’;
    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 ‘</div>’;

    // Start Search Results Map
    if($ct_options[‘ct_disable_google_maps’] == ‘No’) {
    echo ‘<div id=”map-wrap”>’;

    wp_reset_postdata();

    $search_values[‘post_type’] = ‘listings’;
    $search_values[‘paged’] = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;
    $search_values[‘showposts’] = $search_num;
    $wp_query = new wp_query( $search_values );

    search_results_map();

    // End Search Results Map
    echo ‘</div>’;
    }

    // Left Sidebar
    if($layout == ‘left-sidebar’) {
    echo ‘<div id=”sidebar” class=”col span_3 first”>’;
    if (function_exists(‘dynamic_sidebar’) && dynamic_sidebar(‘Listings Search Left’) ) :else: endif;
    echo ‘</div>’;
    } ?>

    <div id=”listings-results” class=”col span_9 <?php if($ct_options[‘ct_listings_layout’] == ‘Grid’) { echo ‘grid’; } else { echo ‘list’; } ?> <?php if($ct_options[‘ct_layout’] == ‘right-sidebar’) { echo ‘first’; } ?>”>

    <?php

    // Reset Query for Listings
    wp_reset_query();
    wp_reset_postdata();

    $search_values[‘post_type’] = ‘listings’;
    $search_values[‘paged’] = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;
    $search_values[‘showposts’] = $search_num;
    $wp_query = new wp_query( $search_values );

    if($ct_options[‘ct_listings_layout’] == ‘Grid’) {
    get_template_part( ‘layouts/grid’);
    } else {
    get_template_part( ‘layouts/list’);
    }

    // End Search Results Map
    echo ‘</div>’;

    // Restore WP_Query object
    $wp_query = $existing_query_obj;

    // Right Sidebar
    if($layout == ‘right-sidebar’) {
    echo ‘<div id=”sidebar” class=”col span_3″>’;
    if (function_exists(‘dynamic_sidebar’) && dynamic_sidebar(‘Listings Search Right’) ) :else: endif;
    echo ‘</div>’;
    }

    echo ‘</div>’;

    get_footer(); ?>

Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘I'm stuck with a search filter (dynamic)’ is closed to new replies.