Append a parametter at first or last to a certain URL

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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;
    }
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x