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
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
OR
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