1

For SEO purposes I want to add a few specific urls to act as aliases to the archive page for a custom post type archive page that I have.

Now the custom post type archive page optionally accepts GET parameters which I then do various things with if they are parsed.

Taking the example below from my functions.php file, the add_rewrite_rule() does work in so far as going to /test/url/ on my site does take me to the archive page for the custom post archive page. So the basic catch is definitely working as I don't get a 404 page.

However the get parameter (param1) doesn't appear to have made the rewrite. It's as if it wasn't defined.

function rewrite_test() {

   add_rewrite_rule('^test/url/?', 'index.php?post_type=custom&param1=value']), 'top');
    }
}
add_action('init', 'rewrite_test');

So what is wrong with this, how can I get the custom url parameters I want parsed via the rewrite rule?

  • Assuming you're getting this param with get_query_var('param1'), you must add param1 to the query variables – Samuel Elh Nov 14 '16 at 22:17
  • Ah fantastic @SamuelElh ! Do you want to write an answer? – AdamJones Nov 14 '16 at 23:03
2

You'll need to append the new variable (param1) to the public query variables:

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

Now you'll be able to get the value of this param with get_query_var( 'param1' ).

|improve this answer|||||
  • One sticking point; I've copied my code to the live server and now it doesn't work there. I've tried adding/removing proceeding and trailing slashes but no change. Any ideas? – AdamJones Nov 19 '16 at 16:20

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.