WordPress rewrite retrieve query var

I’m trying to create a WordPress rewrite rule, but for the life of me, cannot retrieve the query variable.

I have a page called current-releases. I’m trying to add another parameter to the url, for example:
http://localhost/our-wines/current-releases/9031

Read More

In the template for current-releases, I’m trying to get the extra parameter (in this case 9031).

function init() {
  add_rewrite_tag('%wine%', '([^&]+)');
  add_rewrite_rule('^current-releases/([^/]*)/?$','index.php?pagename=current-releases&wine=$matches[1]','top');            
}
add_action( 'init', 'init' );

And then in the template file, trying:

echo $wp_query->query_vars['wine'];
echo get_query_var('wine');

And neither are working.

I have a feeling it’s not matching the regex and not being put into the $matches array()? I’ve tried so many different ways of achieving this, but it just won’t go. The only success I’ve had is if I specify the query variable in the URL such as:
http://localhost/our-wines/current-releases/?wine=9031

Any help with this would be greatly appreciated. Thanks!

Related posts

Leave a Reply