Discussion:
Post name permalinks and default post types
Funkatron
2014-02-26 00:55:57 UTC
Permalink
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!
Simon Blackbourn
2014-02-26 01:19:49 UTC
Permalink
Post by Funkatron
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.
I've just been experimenting with this today. This method is working for
me:
https://vip.wordpress.com/documentation/remove-the-slug-from-your-custom-post-type-permalinks/


Only thing tht might be a problem if there is a post of another post type
Post by Funkatron
with the same name, but I'm not sure how WP handles duplicate post names of
different post types.
The default single.php template is used, but the loop will loop through to
show all posts with the same slug.

Apart from this, the only side effect I have so far discovered is that if
you trash one of the duplicate posts you will get a 404 for the other one
until you delete it permanently.

Cheers
Simon

Continue reading on narkive:
Loading...