propertyhive / WP-Property-Hive

The complete estate agency software plugin for WordPress
Other
27 stars 27 forks source link

Search form field city #189

Closed Ecostack closed 2 years ago

Ecostack commented 2 years ago

Hey guys,

I am looking currently through your documentation and it looks like there is no easy way of adding a drop down field based on properties from the property.

I imagine a search form field drop down automatically filled with all available properties.

From what I see, I would need to iterate over all properties and create my own Search form field via PHP code, is that right?

propertyhive commented 2 years ago

@Ecostack Search forms can be modified using our free Template Assistant add on:

https://docs.wp-property-hive.com/add-ons/template-assistant/managing-search-forms/

All of the available fields can be found there. If the field you're after isn't in that list then it would be a case of writing your own.

Let me know if I can assist further,

Steve

Ecostack commented 2 years ago

@propertyhive Thanks for your answer! I did use the Template Assistant so far but I am missing a specific option.

I would like to dynamically generate options for a search dropdown based on the address city field. Now, from what I see, this is not possible with the Template Assistant.

Ecostack commented 2 years ago

I ended up with the following filter function:

add_filter( 'propertyhive_search_form_fields_after_default', 'after_default_propertyhive_search_form_fields' );
add_filter('propertyhive_address_fields_to_query','edit_default_propertyhive_address_fields_to_query');
function edit_default_propertyhive_address_fields_to_query($fields) {
    return array(
        //'_address_street',
        //'_address_two',
        '_address_three'
        //'_address_four',
        //'_address_postcode'
    );
}
function after_default_propertyhive_search_form_fields($fields) {

    $args = array(
        'post_type' => 'property'
    );

    $post_query = new WP_Query($args);

    $cities = array('' => 'Any');

    if($post_query->have_posts() ) {
        while($post_query->have_posts() ) {
           $post_query->the_post();
            $property = new PH_Property( get_post()->ID );

            if ($property->post_status == "publish" && $property->on_market == 'yes'){
                $cities[$property->_address_three] = $property->_address_three;
            }

            }
        }

    $fields['address_keyword'] = array(
       'type' => 'select',
        'show_label' => true,
        'label' => 'Location',
        'options' =>$cities
    );

    return $fields;
}