WordPress URL rewrite with custom variables not working
Question
I have a website that allows users to create and browse listings.
What I have:
listings/for-sale/?c=thecategory&co=belgium&....
What I want:
listings/for-sale/thecategory/?co=belgium&....
This is the code I currently have:
function wpa_query_vars( $query_vars ){
$query_vars[] = 'c';
return $query_vars;
}
add_filter('query_vars', 'wpa_query_vars');
function add_rewrite_rules($rules) {
$newrules['listings/for-sale/([^/]+)/?$'] = 'index.php?pagename=for-sale&c=$matches[1]';
$rules = $newrules + $rules;
return $rules;
}
add_filter('rewrite_rules_array', 'add_rewrite_rules');
The problem is that this code removes the c
variable entirely for some reason.
Instead of returning
listings/for-sale/thecategory/?co=belgium&...
I now get
listings/for-sale/?co=belgium&....
Anyone can point me in the right direction and sees what’s wrong with my code?
0
url-rewriting
3 years
2020-03-24T12:51:12-05:00
2020-03-24T12:51:12-05:00 0 Answers
93 views
0
Leave an answer