Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have successfully added a custom post type and the permalink structure works great. So, for instance, I've got a custom post type books, and the permalink to a book is /books/the-dictionary.

My single-book.php page template accepts a letter parameter to customize the content displayed on the page. I'd like to be able to link to this like /book/the-dictionary/q.

I've added the following to my theme's functions.php file:

function my_init() {

function add_books_rewrite() {
    add_rewrite_rule("books/([^/]+)/([^/])/?$", 'index.php?pagename=$matches[1]&letter=$matches[2]', "top");
    global $wp_rewrite;
function add_books_query_vars($query_vars) {
    $query_vars[] = "letter";
    return $query_vars;
add_filter("init", "my_init");
add_filter("query_vars", "add_books_query_vars");

It took a few tries with using the flushing code above, manually going to the permalink settings page and saving, and fixing stupid errors in the regex, but eventually I was able to verify that the rewrite was added correctly by using Jan Fabry's fabulous rewrite analyzer plugin.

confirmed in rewrite analyzer

So, from here I think it's smooth sailing, right? Yeah...not exactly.

When I load up my site and go to books/the-dictionary/q, it does load the-dictionary content correctly, but it is losing the parameter. The thing that I find interesting is that if I load index.php?pagename=the-dictionary&letter=q, everything works great.

To retrieve the parameter, I have the following code:

global $letter;
echo $letter. " - global";
echo "<br>";
$current_letter = (isset($wp_query->query_vars["letter"]) ? $wp_query->query_vars["letter"] : "all");
echo $current_letter . " - query_vars";
echo "<br><br>";

I've also been looking at $_SERVER["QUERY_STRING"]. These are populated when I use index.php?pagename...&letter..., but not when I use /books/pagename/letter.

So, it seems like a few things are happening here. WordPress isn't completely dropping the parameter since things work if I specify index.php with the appropriate querystring variables. This seems to indicate the rewrite rule isn't working correctly. However, the Rewrite Analyzer plugin seems to say things are set up properly for what I'm going for.

I'm a bit confused at what I should be looking at here to fix the disconnect. Sorry if I'm missing something obvious. ):

share|improve this question
How are you retrieving the parameter in you page template? Perhaps the issue is there. –  dalbaeb Aug 22 '11 at 14:38
Are you sure it's something with the rewrite? And something in single-book.php? How are you getting the letter variable in that file? –  chrisguitarguy Aug 22 '11 at 14:40
@dalbaeb Ah, this should have been in the original post. I've edited it in. –  Rebecca Chernoff Aug 22 '11 at 14:46
@ChristopherDavis Ah, this should have been in the original post. I've edited it in. –  Rebecca Chernoff Aug 22 '11 at 14:47
add comment

1 Answer

I think it's probably your front end code that's the problem.

Try this:

if( $letter = get_query_var( 'letter' ) ) 
        echo 'Current Letter: ' . $letter;
        echo 'Current Letter: All';
share|improve this answer
Nope. ): The url still ends up as /books/the-dictionary and all (now 3) ways of pulling the qs var are empty. –  Rebecca Chernoff Aug 22 '11 at 14:58
This is sort of a long shot, but replace your first two functions in functions.php (my_init & add_books_rewrite) with this: pastie.org/2411828 and delete your add_filter('init' ...) line. Then try manually flushing your rewrite rules by going to your permalinks settings page and hitting save. –  chrisguitarguy Aug 22 '11 at 15:18
And you may want to try deleting the dollar sign at the end of your add_rewrite_rule regex as well. –  chrisguitarguy Aug 22 '11 at 15:21
So you think the Rewrite Analyzer indicating things are ok is a red herring? Hmm, will try. –  Rebecca Chernoff Aug 22 '11 at 15:52
Yeah...no change. –  Rebecca Chernoff Aug 22 '11 at 15:54
show 1 more comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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