WordPress URL rewrite with custom variables not working


I have a website that allows users to create and browse listings.

What I have:


What I want:


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


I now get


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>
Senne Vandenputte 2 months 0 Answers 74 views 0

Leave an answer