I am creating a messaging system where I have 4 sections (1)inbox (2)sent (3)draft (4)unread so I decide to go with WordPress add rewrite rules

I have already added one rewrite rule section.

'top' );

Now I want to add one more param after the section is the ID(inbox messages id).

so for that, I add a second rewrite rule id.

'top' );

if I visit https://example.com/messages/inbox I can get in query_vars

[section] => inbox 

but the issue is that if I visit https://example.com/messages/inbox/123 I can get in query_vars

[section] => 
[id] => 123

I am not getting section value, in this case, I want to check in this URL which section Is currently that's why I need section value on this page.

  • 1
    $matches[2] in your second rewrite rule doesn't match any pattern, so you should just use &section=inbox there.
    – Sally CJ
    Apr 23, 2020 at 0:49
  • @SallyCJ Yes, thank you. Your solution is working. but I doubt that this way is right or not can you suggest a better way to achieve what I describe. or it's possible to get all params in last id page I mean can I get section dynamically in id page so I can check which section is currently in id page cause it's possible section are "inbox/id" or "sent/id" or "draft/id" and "unread/id".
    – Bhautik
    Apr 23, 2020 at 8:26
  • 1
    In that case, you can use something like messages/(inbox|sent|draft|unread)/(\d+)/?$ with &section=$matches[1]&id=$matches[2].
    – Sally CJ
    Apr 26, 2020 at 12:32
  • 1
    @SallyCJ Thank You. it's worked as I want.
    – Bhautik
    Apr 28, 2020 at 6:29

1 Answer 1


See this is sort of solved in the comments and old but to save someone going down the wrong path here, I believe this is done the wrong way for OP to achieve what they want.

One re-write rule should be enough here, and then use the matches to construct your query.

Firstly, we need a Regex that will match the whole string of what you're trying to parse

Something like this will work "/^messages\/(inbox|sent|draft|unread)\/?(\d*)?$/"

That starts with messages/ has any of (inbox|sent|draft|unread) in the middle, an optional slash and a second optional match group of numbers at the end.

This will allow your rewrite rule to work as follows using your $matches


See this page in the codex for more information and examples
add_rewrite_rule() | Function | WordPress Developer Resources

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.