Laurence Cope
Laurence Cope

Reputation: 422

add_rewrite_rule not working when referencing child page name as matches

Is anyone able to comment why this does not work.

I have created a custom post type called "knowledge-hub". I also then created a page called "knowledge-hub" to act as the landing page for my CPT listings, which I show using code. On one of the CPT pages, "minimum-wage-policies", I want to be able to access a variable in the URL, e.g. ?some_var=xyz. But I want it friendly like /knowledge-hub/minimum-wage-policies/xyz.

So I have created rewrite URLs. The page name "minimum-wage-policies" will vary for many of the CPT pages, so I cant hard code this. I want to access it using a URL part.

But I cant get the add_rewrite_rule() to match matches[1] to the page name. If I hard code this page name in the add_rewrite_rule it works! But as soon as I replace it with matches[1] it does not work, its either a 404 or a redirect back to minimum-wage-policies with no variable on the end.

URL would be: http://my-domain.com/knowledge-hub/minimum-wage-policies/some_var

Referencing the child page name works:

add_rewrite_rule(
    'knowledge-hub/([^/]*)?/([^/]*)/?$',
    'index.php?post_type=knowledge-hub&pagename=minimum-wage-policies&my_var=$matches[2]',
    'top'
);

matches[2] is working in this case as I can see the value passed in the URL in my code.

But using matches[1] does not work and returns a 404:

add_rewrite_rule(
    'knowledge-hub/([^/]*)?/([^/]*)/?$',
    'index.php?post_type=knowledge-hub&pagename=$matches[1]&my_var=$matches[2]',
    'top'
);

Visiting the WP URL http://my-domain.com/index.php?post_type=knowledge-hub&pagename=minimum-wage-policies&my_var=xyz works, so that structure is OK.

I have tried various regex expressions used by various others on the internet, so not just ([^/])?. So am not sure if it is to do with the regex and I just have not found the right one. I want to match anything. Same issue with (.)

I also have this:

function add_my_vars($vars)
{
    $vars[] = 'my_var';
    return $vars;
}
add_filter('query_vars', 'add_my_vars');

Update:

Using $request->matched_query I could see the URLs being called. When I use pagename=minimum-wage-policies the URL is as expected:

post_type=knowledge-hub&pagename=minimum-wage-policies&my_var=xyz

When I use matches[1] is completely changed which is quite bizarre:

knowledge-hub=minimum-wage-policies%2Fxyz&page=

Upvotes: 0

Views: 277

Answers (1)

Laurence Cope
Laurence Cope

Reputation: 422

I managed to get it working with this rewrite_rule but I have not found this structure online so not sure if its technically correct but it works. Note the use of knowledge-hub=$matches[1]. This was discovered from viewing the $request->matched_query I mentioned was bizarre above. Replicating it in the rewrite_rule makes it work.

add_rewrite_rule(
    'knowledge-hub/([^/]*)?/([^/]*)/?$',
    'index.php?knowledge-hub=$matches[1]&my_var=$matches[2]',
    'top'
);

Upvotes: 0

Related Questions