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?

--------------------------------------------------

>>> Share a link to this question, just copy and paste the code bellow on your web page <<<

<a href="https://www.wptricks.com/question/wordpress-url-rewrite-with-custom-variables-not-working/">

Wordpress URL rewrite with custom variables not working</a>
0
Senne Vandenputte 2 months 0 Answers 74 views 0

Leave an answer