Difficulty with rewrite rules

I can’t work out how to combine a rewrite rule and a custom query var.

I have created a custom ‘edition’ template, which calls an external API to populate a table with details of a particular book.

https://wwww.example.com/books/editions

I use a custom query var to populate the database:

https://www.example.com/books/editions/?ed=1950

for example, where ed is a unique identifier, normally the year, for an edition.

How do I rewrite this (using a WordPress function), to:

https://www.example.com/books/editions/1950

I have tried many things from the codex, but with no success! So far:

Functions.php:

function mr2018_custom_query_vars_filter($vars) {
$vars[] .= ‘ed’;
return $vars;
}
add_filter( ‘query_vars’, ‘mr2018_custom_query_vars_filter’ );

MyCustomTemplate.php:

$edition = get_query_var( ‘ed’ );

This works: I can use ?ed=1603 in MyCustomTemplate.php to call the database. Where I am stuck is rewriting this to something readable, and SEO friendly.

I believe there are functions which will combine these two processes, but I’m not sure which to use in this instance.

Read more here:: Difficulty with rewrite rules

Leave a Reply

Your email address will not be published. Required fields are marked *