1. Home
  2. Docs
  3. Developer Guide
  4. Miscellaneous and Snippet...
  5. Different URLs For Different Searches

Different URLs For Different Searches

Video Guide: https://komododecks.com/recordings/9ED0yC17TbK11e9q9mIN

By default there is one page that you use as your ‘Property Search’ page. This is specified in the ‘General’ section of the Property Hive settings area.

Sometimes you want to offer the users more friendly URLs. For example you might want to use /sales as the URL for displaying properties for sale, and /lettings to display properties to rent.

The above scenario can be achieved by adding the following to your theme’s functions.php file:

// Setup two separate URLs
add_action( 'init', 'ph_add_rewrite_rules' );
function ph_add_rewrite_rules() 
{
    global $wp_rewrite;
     
    // Setup Sales URL '/sales/'
    add_rewrite_rule( "sales/?$", "index.php?post_type=property&department=residential-sales", 'top' );
    add_rewrite_rule( "sales/{$wp_rewrite->pagination_base}/([0-9]{1,})/?$", "index.php?post_type=property&department=residential-sales" . '&paged=$matches[1]', 'top' );
     
    // Setup Lettings URL '/lettings/'
    add_rewrite_rule( "lettings/?$", "index.php?post_type=property&department=residential-lettings", 'top' );
    add_rewrite_rule( "lettings/{$wp_rewrite->pagination_base}/([0-9]{1,})/?$", "index.php?post_type=property&department=residential-lettings" . '&paged=$matches[1]', 'top' );
}
 
// Add support for Property Hive custom query_vars
add_filter( 'query_vars', 'add_query_vars' );
function add_query_vars( $query_vars ) 
{
    $query_vars[] = 'department';
    $query_vars[] = 'address_keyword';
    $query_vars[] = 'marketing_flag';
    $query_vars[] = 'minimum_price';
    $query_vars[] = 'maximum_price';
    $query_vars[] = 'minimum_rent';
    $query_vars[] = 'maximum_rent';
    $query_vars[] = 'minimum_bedrooms';
    $query_vars[] = 'minimum_floor_area';
    $query_vars[] = 'maximum_floor_area';
    $query_vars[] = 'property_type';
    $query_vars[] = 'commercial_property_type';
    $query_vars[] = 'officeID';
    $query_vars[] = 'view';
    $query_vars[] = 'radius';
    $query_vars[] = 'pgp';
    // append here any other query string parameters you might be using
 
    return $query_vars;
}
 
// Convert all query_vars found to $_GET and $_REQUEST parameters as this is what the search form and main property query are based off
add_action( 'parse_request', 'setup_get' );
function setup_get($wp_query)
{
    foreach ($wp_query->query_vars as $name => $value)
    {
        if (!empty($value) && $name != 'name')
        {
            $_GET[$name] = $value;
            $_REQUEST[$name] = $value;
        }
    }
}

Important: Once you’ve added the above, remember to go into ‘Settings > Permalinks‘ and just hit ‘Update’ to force the rewrites to take effect.

 

Retaining the URL when a search is performed

If you have a search form on your results page you'll likely notice that once a search is performed that you lose the URL's specified in your rewrite rules above.

To get around you can override the /global/search-form.php template and change the action of the form.

By default this will be like so:

echo get_post_type_archive_link( 'property' );"

.. .but could be updated like so:

if ( isset($_GET['department']) && $_GET['department'] == 'residential-lettings' ) { echo '/lettings/'; }else{ echo '/sales/'; }"
Was this article helpful to you? No 1 Yes

How can we help?