I'm working on a project with several custom post types, where one of the post types is naturally (but not actually in the WordPress sense) the 'parent' of the others. For example, say I have a custom post type called 'book' and another one called 'character', and say I want to have the following custom URL structure:
books/[book-name]/characters/[character-name]
Thus, the canonical URL for the post about the character Thing One in the book The Cat In the Hat would be:
books/cat-in-the-hat/characters/thing-one
A character is associated with a book with a post meta row.
I've created custom rewrites to make it work...
add_rewrite_rule (
'^books/([^\/]+)/characters/([^\/]+)/?$',
'index.php?book=$matches[1]&character=$matches[2]&post_type=character'
);
...but I'd also need to make sure that the 'character post' in the URL belongs to the 'book'...
books/cat-in-the-hat/characters/thing-one //good
books/anna-karenina/characters/count-vronsky //good
books/anna-karenina/characters/thing-one //bad
This can't happen automatically: I have to hook into a filter or an action to check whether the character matches the book, and if not, either redirect to the correct, canonical URL or force a 404.
Where's the best place to accomplish this check? My choices (so far) are:
- The
request
filter -- i.e. checking before the main query is instantiated. - The various WP_Query filters -- i.e. make WP_Query do the checking for me by adding a post meta constraint.
I realize this is rather an open-ended question and probably a matter of taste, but any insight into the most code and database efficient way of doing things would be appreciated. Thanks.
post meta row
which I can only assume when the user creates a new book, they somehow find / pick the character(s) attached to it, which is then saved as meta values attached to the book post ID. That being said, I am confused with OPs final example - the last url being bad assumes that 2 different books cannot have the same character names.