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. ):

  • How are you retrieving the parameter in you page template? Perhaps the issue is there. Commented Aug 22, 2011 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? Commented Aug 22, 2011 at 14:40
  • @dalbaeb Ah, this should have been in the original post. I've edited it in. Commented Aug 22, 2011 at 14:46
  • @ChristopherDavis Ah, this should have been in the original post. I've edited it in. Commented Aug 22, 2011 at 14:47

1 Answer 1


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';
  • Nope. ): The url still ends up as /books/the-dictionary and all (now 3) ways of pulling the qs var are empty. Commented Aug 22, 2011 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. Commented Aug 22, 2011 at 15:18
  • And you may want to try deleting the dollar sign at the end of your add_rewrite_rule regex as well. Commented Aug 22, 2011 at 15:21
  • So you think the Rewrite Analyzer indicating things are ok is a red herring? Hmm, will try. Commented Aug 22, 2011 at 15:52
  • Yeah...no change. Commented Aug 22, 2011 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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