3

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

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?

2

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.

  • Thank you so much! that did the trick. I do find it strange that wordpress was able to find the correct PAGE, even though i was rewriting to a POST. But now my query var works. – Malibur Sep 23 '13 at 6:17
  • I do have one question though. When i generate permalinks to these pages how do i then make sure that it will work no matter wether "pretty permalinks" is turned on or not. If i link to albums/vinyl - then it only works if prettyP is on, And if i link to ?page_id=97&type=vinyl - it only works when prettyP is off. What is the best practice to get around that? – Malibur Sep 23 '13 at 6:20
  • what you were seeing was your request resulting in a 404, which then causes WP to run the canonical redirect function, which does a LIKE query for your request and forwards there if something similar is found. page_id should work regardless of pretty permalinks being on or off, but you can also check the state of permalinks by looking at the option permalink_structure, it will be empty if pretty permalinks are off. – Milo Sep 23 '13 at 13:04
  • yeah "page_id" works because its native wordpress, but how do i get "type" to behave the same way? has that got to do with the permalink_structure? im just wondering what a permalink should end up looking like in the code. probable something lige: <a href="<?php the_permalink(); ?>vinyls">link</a> or what? thats not suppose to work with permalinks off is it? – Malibur Sep 23 '13 at 13:37
  • I think you'll have to build them manually by checking if the permalink_structure option is not empty, otherwise use add_query_arg to append type. – Milo Sep 23 '13 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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