I have the following rewrite rules that are hooked into the init action hook.

// This works
add_rewrite_rule( '^books/fiction/?', 'index.php?post_type=book&book_type=fiction', 'top' );
add_rewrite_rule( '^books/nonfiction/?', 'index.php?post_type=book&book_type=nonfiction', 'top' );

// This doesn't work
add_rewrite_rule( '^books/fiction/([^/]*)/?$', 'index.php?pagename=$matches[1]&post_type=book&book_type=fiction', 'top' );
add_rewrite_rule( '^books/nonfiction/([^/]*)/?$', 'index.php?pagename=$matches[1]&post_type=book&book_type=nonfiction', 'top' );

The first two rewrite rules are for the archive page, where I've split the archive page into two sections, either fuction or non-fiction for the books.

That works fine.

However, I want to add the type of books to the URL of the single book (post page) but it's not working. When I go to, for example:

example.com/books/fiction/to-kill-a-mockingbird, it takes me to the example.com/books/fiction/ page instead (keeping the book slug in tact still though in the URL).

How can I ensure this works alongside the existing rewrite rules for the archive pages? I have saved the permalinks page to flush the rewrite rules too.

1 Answer 1


the URL index.php?pagename=to-kill-a-mockingbird&post_type=book&book_type=fiction works only for simple post or page.
when you have a custom post type, the URL is index.php?book=to-kill-a-mockingbird&book_type=fiction.

then try this code :

    , 'index.php?book=$matches[1]&book_type=fiction'
    , 'top'
  • Thank you. I figured out that I also needed to put my post rewrite rules before my archive page rewrite rules and then it started working.
    – user147070
    Jul 17, 2018 at 10:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy