Why does wordpress still strip my query var?

Im trying to pass a query var to a custom page template with pretty permalinks.

So basically i have a page called “albums”. And i want to pass a query var to that page called “type”. So the non-pretty version would be mywebsite.com/index.php?p=97&type=vinyl

Read More

the “pretty” version would the be mywebsite.com/albums/vinyls

And the template need to then retrieve that variable (i.e. vinyls)

I have read a lot on this all day, but still cant get it to work. So i was hoping some kind soul here would help me out.

So first im adding the rewrite to my functions.php:

function addrewrite() {
    add_rewrite_rule( '^albums/([^/]+)/?$',
    'index.php?p=97&type=$matches[1]',
    'top' );
}
add_action('init', 'addrewrite');

This seems to work as i can see it turn up if a get a var_dump of all the rewrites.

Now if i do a test and type mywebsite.com/albums/vinyl into my browser
wordpress strips the last bit “vinyl” away. I read a couple of places that this is because i need to register the query var too. so i did that with the following code:

add_filter('query_vars', 'add_my_var');
function add_my_var($public_query_vars) {
  $public_query_vars[] = 'type';
  return $public_query_vars;
}

Then i flushed the permalinks by pressing save in the settings -> permalinks, yet wordpress still strips my url.

What am i missing here?

Related posts

1 comment

  1. For a page, change p=97 to page_id=97 or pagename=albums. p is for the post post type.

    I also suggest changing your query var to something more unique.

Comments are closed.