0

I have a basic redirection set up:

function example_add_rewrite_rules() {
  $year = '(\d{4})?';
  $make = '([a-z]{1,20})';
  $model = '(.+)';
  add_rewrite_rule( 'c/'.$year.'/?'.$make.'/'.$model.'/?$', 'index.php?page_id=46&year=$matches[1]&make=$matches[2]&model=$matches[3]', 'top' );
}
add_action( 'init', 'example_add_rewrite_rules' );

My hope is that whenever the user hits the page with a URL of ../c/2015/ford/f150, that URL is stored in a query string.

So, hopefully, the user will see this: "/c/2015/ford/f150"

While I see this: "?page_id=46&year=2015&make=ford&model=f150"

I cant tell if its working or not though. If I type in /c/2015/ford/f150 it will redirect me to the correct page/page_id. But I cant figure out for the life of me how to print those query variables to the page so I can at least see that the query string is configured correctly and has variables stored in year, make, and model.

Summary: Have I done anything wrong in the code above? If not, how do I print the query string variables to the page?

Aside: I'm jumping into an ocean of PHP and I'm still trying to learn how to swim. If I misstated something, forgive and correct me, for I know not what I do.

0

First, year is already a WordPress query var used for date-based archives, you should change that to something unique to prevent possible conflicts.

To use your own query vars within rewrite rules, you need to add them to the list of known vars:

function wpd_query_vars( $qvars ) {
  $qvars[] = 'my_year';
  $qvars[] = 'make';
  $qvars[] = 'model';
  return $qvars;
}
add_filter( 'query_vars', 'wpd_query_vars' , 10, 1 );

Then you can output their values in the template with get_query_var:

echo get_query_var( 'model' );
| improve this answer | |
  • You Rock So HARD!! – ExcellentSP Jan 14 '15 at 18:07

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.