I have a CPT called presskit
, and I need to add a custom query var named welcome_box
. I have this code right now:
function my_new_url_querystring(){
add_rewrite_tag( '%welcome_box%','([^&]+)' );
add_rewrite_rule(
'presskit/(.?.+?)/box/(.+)/?$',
'index.php?post_type=presskit&pagename=$matches[1]&welcome_box=$matches[2]',
'top'
);
}
add_action( 'init', 'my_new_url_querystring' );
When I try to open mysite.com/presskit/presskit-name/box/john-doe
, it gets back to mysite.com/presskit/presskit-name/
.
If I open mysite.com/presskit/presskit-name/?welcome_box=john-doe
, It works and I can get the query var.
What am I doing wrong? I'm not good with regexes, so maybe the erros is there.
Any tips are welcome!
Update
Thanks to the @Milo answer, I could make it work. Here's the working code:
function my_new_url_querystring(){
add_rewrite_tag( '%welcome_box%','([^&]+)' );
add_rewrite_rule(
'presskit/([^/]+)/message-to/(.*)?/?$',
'index.php?presskit=$matches[1]&welcome_box=$matches[2]',
'top'
);
}
add_action( 'init', 'my_new_url_querystring' );