I have defined a rewrite rule for a subpage 'vehiculo' (which is a template-page in my theme folder), and subpage of 'vehiculos', because I want a pretty URL and the code is handled in the template file. So the target is to handle https://mydomain.cl/vehiculos/vehiculo/1599 as https://mydomain.cl/vehiculos/vehiculo/?auto-id=1599 or https://mydomain.cl/index.php?pagename=vehiculo&auto-id=1599

function query_vars_filter($vars) {
  $vars[] = 'id';
  $vars[] = 'auto-id';
  return $vars;
add_filter( 'query_vars', 'query_vars_filter' );

function custom_rewrite_rule() {
add_action('init', 'custom_rewrite_rule', 10, 0);

insofar the rewrite should theoretically work, as the rewrite analyzer shows: rewrite analyzer

Also when I paste the URL https://mydomain.cl/index.php?pagename=vehiculo&auto-id=1599 manually into the browser it gives me the right result. I have made the code so flexible that https://mydomain.cl/vehiculos/vehiculo/?auto-id=1599 also works

So far, so good, right? Now the crux is: if I browse to https://mydomain.cl/vehiculos/vehiculo/1599 The correct page is shown, but somehow the parameter 1599 is lost. I var dumped the query string, get params and apparently 'auto-id' is nowhere found, which is a complete mistery to me.

$_GET = array(0) { }  $wp_query->query_vars = array(66) { ["page"]=> int(0) ["pagename"]=> string(8) "vehiculo" ["error"]=> string(0) "" ["m"]=> string(0) "" ["p"]=> int(0) ["post_parent"]=> string(0) "" ["subpost"]=> string(0) "" ["subpost_id"]=> string(0) "" ["attachment"]=> string(0) "" ["attachment_id"]=> int(0) ["name"]=> string(8) "vehiculo" ["page_id"]=> int(0) ["second"]=> string(0) "" ["minute"]=> string(0) "" ["hour"]=> string(0) "" ["day"]=> int(0) ["monthnum"]=> int(0) ["year"]=> int(0) ["w"]=> int(0) ["category_name"]=> string(0) "" ["tag"]=> string(0) "" ["cat"]=> string(0) "" ["tag_id"]=> string(0) "" ["author"]=> string(0) "" ["author_name"]=> string(0) "" ["feed"]=> string(0) "" ["tb"]=> string(0) "" ["paged"]=> int(0) ["meta_key"]=> string(0) "" ["meta_value"]=> string(0) "" ["preview"]=> string(0) "" ["s"]=> string(0) "" ["sentence"]=> string(0) "" ["title"]=> string(0) "" ["fields"]=> string(0) "" ["menu_order"]=> string(0) "" ["embed"]=> string(0) "" ["category__in"]=> array(0) { } ["category__not_in"]=> array(0) { } ["category__and"]=> array(0) { } ["post__in"]=> array(0) { } ["post__not_in"]=> array(0) { } ["post_name__in"]=> array(0) { } ["tag__in"]=> array(0) { } ["tag__not_in"]=> array(0) { } ["tag__and"]=> array(0) { } ["tag_slug__in"]=> array(0) { } ["tag_slug__and"]=> array(0) { } ["post_parent__in"]=> array(0) { } ["post_parent__not_in"]=> array(0) { } ["author__in"]=> array(0) { } ["author__not_in"]=> array(0) { } ["search_columns"]=> array(0) { } ["ignore_sticky_posts"]=> bool(false) ["suppress_filters"]=> bool(false) ["cache_results"]=> bool(true) ["update_post_term_cache"]=> bool(true) ["update_menu_item_cache"]=> bool(false) ["lazy_load_term_meta"]=> bool(true) ["update_post_meta_cache"]=> bool(true) ["post_type"]=> string(0) "" ["posts_per_page"]=> int(10) ["nopaging"]=> bool(false) ["comments_per_page"]=> string(2) "20" ["no_found_rows"]=> bool(false) ["order"]=> string(4) "DESC" }

Any clou why that happens? where could the auto-id be? Is it lost because there's a second rewrite rule matching?

2 Answers 2


That's because in the URL https://mydomain.cl/vehiculos/vehiculo/1599 there are no query parameters, and no GET parameters.

This is because you rewrote them into the URL, and they are now query variables. For the same reason $_GET['pagename'] wouldn't work, neither will $_GET['auto-id'].

Instead, use get_query_var( '....' );.

  • that's what i do in the code, but as you can see from the post, I do var_dump $wp_query->query_vars and there is no auto-id in it. That's what buffles me. I tried to extend the add_rewrite rule with a vehiculos/vehiculo/([^/]*)/?$ question mark at the end, with no success neither. even though the reqrite analyzer shows me the param, the query vars seem not contain auto-id Jul 18 at 17:28
  • Also at the same time, if I write the index.php?pagename=... part manually into the browser, the behaviour is correct, only when going through the rewrite rule the auto-id is not contained in the query vars. I now even tried to change the sequence putting the query_vars_filter in the function before the rewrite rule, but no success neither. Jul 18 at 17:39
  • I read your question and could see no dumping of the query vars array, only $_GET. Note that nowhere in your question do you show the place where you try to retrieve the value, but again, since you're using rewrite rules it is no longer a GET value/URL parameter. The behaviour you're describing heavily implies that you've written $_GET['auto-id'] somewhere
    – Tom J Nowell
    Jul 19 at 11:07
  • Thx Tom, in the last paragraph I put (after the _GET) the output of $wp_query->query_vars on the target page, it's one long line. I still believe the problem is that I wanted to use the same path for the rewrite: /vehiculos/vehiculo/1599 > vehiculos/vehiculo & query_var 1599 which probably messes up something, the canonical path being /vehiculos/vehiculo/1599/ Jul 20 at 16:05

Thanks for the additional push. It made me dive deeper and with the plugin Query Monitor I came to the culprit. The original problem was that though the first rewrite as stated in the post worked, a second rewrite got applied with a standard rule because original and rewrite path were too similar: /vehiculos/vehiculo/1599 was forwarding to index.php?pagename=vehiculo&auto-id=1599 which then was forwarding to /vehiculos/vehiculo/ and somehow lost the query-var

I still don't fully understand the underlying culprit, but somehow this made the query-var disappear. And thinking about it kind of made sense, because I was defining a pretty-url exactly the same as the ugly-url path which probably created the confusion.

So when rewriting the pretty print url from /vehiculos/vehiculo/1599 to just /vehiculo/1599 I created a unique url /vehiculo avoiding thus the path confusion


Note, that the pagename now has the double path in it, and this gives now an unique pretty-print path: /vehiculo/1599 to be forwarded to /vehiculos/vehiculo/?auto-id=1599 and that works

  • WP won't use the rule that matches best, it'll use the rule that matches first, it sounds like an ordering issue. You should also look into the canonical redirect functionality. If you write a fancy rewrite rule to match posts, even if it knows and maps to the correct posts, if that new URL is not the canonical URL WP will redirect it to avoid SEO penalties. You should mention the redirect in your question as it's very important information that's missing, it's very likely that WP is trying to redirect to the canonical URL for that page
    – Tom J Nowell
    Jul 19 at 11:11
  • you may be right... in any case the canonical path gets produced dinamically and is in the form of /vehiculos/vehiculo/$auto-id/ But I wonder if the issue is that the rewrite actually is using the same path just rewriting the URL parameter auto-id from path-elemento to param. Once I've solved other issues I probably recheck this. THX again for the sharing of your knowledge, as there are still some things I do not fully understand in the rewrite system Jul 20 at 16:11

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.