0

Background

I have a a page called 'listen' with a custom template, which runs a custom wp_query that uses the 'show' variable in the URL. The following example works...

example.com/listen/?show=thebigshow

What I Want

I want to be able to get rid of the ?show= part of the url, and format it as follows...

example.com/listen/thebigshow

What I've Tried

I have tried implementing the following rewrite rule, but to no avail.

function add_query_vars_filter( $vars ){
  $vars[] = "show";
  return $vars;
}
add_filter( 'query_vars', 'add_query_vars_filter' );

function listen_rewrite_action() {
  add_rewrite_tag('%show%','([^/]*)');
  add_rewrite_rule(
    '^listen/([^/]+)$',
    '/listen/?show=$matches[1]',
    'top'
  );

}
add_action( 'init', 'listen_rewrite_action' );

A Bit More Information

Rewrite Rules (as previewed in a plugin) Rewrite Rules (as previewed in a plugin)

What .htaccess looks like after generation What .htaccess looks like after generation

Thanks, Andy.

2
  • Just a wild guess, have to tried flushing and regenerate the rewrite rules database after adding this? Just go to Permalinks and hit "Save". I can't see any issues with your rewrite rules. If flushing don't work, try changing the $after parameter to bottom and flush again.
    – Abhik
    Oct 28, 2015 at 8:38
  • I tried flushing and changing 'top' to 'bottom. The rewrite remains inactive. I am using roots starter framework, I'll check to see if anything funky is going on there. Oct 28, 2015 at 8:57

1 Answer 1

2

I manually deleted the .htaccess file and regenerated, which along with the code below, made the rewrite work. The second rewrite rule in the code below makes the pagination work properly.

The .htaccess file doesn't seem to be getting written with any rules though, but the rewrite is working now anyhow.

function listen_rewrite_action() {
  add_rewrite_tag('%show%','([^/]*)');
  add_rewrite_rule(
    '^listen/([^/]+)$',
    'index.php?pagename=listen&show=$matches[1]',
    'top');
  add_rewrite_rule(
    '^listen/([^/]+)/page/([^/]+)$',
    'index.php?pagename=listen&show=$matches[1]&paged=$matches[2]',
    'top');
}
add_action( 'init', 'listen_rewrite_action' );

Your Answer

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

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