Question

I want to change a URL and want to add a parameter to the last or first of that URL. For example I have URL like below

https://example.com/search/?cityId=49409&bedrooms=0&bathCount=0&status=active&sort=priceDesc&boardId=270

I want to append an extra parameter "&propertyType=RNT" the URL at last or first or in the middle whatever So the URL looks like below

https://example.com/search/?&propertyType=RNT&cityId=49409&bedrooms=0&bathCount=0&status=active&sort=priceDesc&boardId=270

OR

https://example.com/search/?cityId=49409&bedrooms=0&bathCount=0&status=active&sort=priceDesc&boardId=270&propertyType=RNT

How can I achieve this ? The solution can be with PHP or using htaccess.

Thanks in advance

Was it helpful?

Solution

I got my code working now. Here is the below code that append that extra parameter to the end of the URL specified above.

    add_action('init', 'add_custom_query_for_search');
function add_custom_query_for_search(){
    global $wp;
    
    $tempUrl = home_url() . add_query_arg( $wp->query_vars ) . '&propertyType=RNT';
    
    if( ! isset($_GET['propertyType']) && (strpos($tempUrl, 'homes-for-sale-search') !== false) ){
        wp_redirect($tempUrl);
        die;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top