[wp-hackers] Post name permalinks and default post types

Funkatron funkatronic at gmail.com
Wed Feb 26 00:55:57 UTC 2014


So, I was combing the internet on how to implement the elusive%postname%
permalink for a custom post type.  I know that it can be done without a
performance hit now but I  was running into issues on how to implement it.
 Everything I tried either didn't work or worked but screwed up permalinks
for pages.  I think I have a solution but I wanted to run it by a few
people to make sure I don't inadvertantly cause any issues:

Basically, I've found that the reason post name permalinks are so tricky is
that WordPress doesn't know what post type to use.  Any rewrite I did would
override page's rewrite or cause WP_Query to default to post.  So, after
much problems and testing, I did 3 things:

1. I set the post type in question to have rewrite set to false

2. Used add_permastruct to create the post name permastructure so WP can
create the permalink

3. (the part I'm worried about) used the parse_query to resolve the
conflict issue:
$post_name = $query->get( 'name' );
$post_type = $query->get( 'post_type' );
if( ! empty( $post_name ) && empty( $post_type ) ) {
$query->set( 'post_type', array( 'post', 'page', 'custom_post_type' ) );
}

So basically, if name is set and post type isn't set, set the post type to
post, page and the custom post type.

My question is can anyone with greater knowledge than I to the inner
workings of WP, think of any unforeseen side effects to doing it this way?
 So far it seems fine to me; doesn't screw up pages or my custom post type.
 Only thing tht might be a problem if there is a post of another post type
with the same name, but I'm not sure how WP handles duplicate post names of
different post types.

Thank you for any comments!


More information about the wp-hackers mailing list