Removing the Slug in a Custom Post Type Permalink

For a long time, having a slugless permalink aka a postname permalink was a luxury feature that only posts and pages in WordPress shared. Though the performance issues that plagued using the postname permalink was resolved with WordPress 3.3, it was still sort of tricky to implement it. I found a few tutorials online but I decided to find my own way of doing it.

  1. Create your post type. Simple enough:

    function event_activate() {
    /**
    * Register a custom post type, set rewrite to false
    */
    view raw sluglesspermalink hosted with ❤ by GitHub

    Notice how I set rewrite to false. This disables rewrite functionality, which is okay, because we’ll add it next.

  2. Add your permalink structure:

    /**
    * Set the permalink structure
    */
    view raw sluglesspermalink hosted with ❤ by GitHub

    This will add the permalink structure for your post type. First argument is the post type name, second is the permastruct, which WordPress will use to create permalinks for your post type.

  3. Resolve the conflict with post and page post types:

    }
     
    add_action( 'init', 'event_activate' );
    /**
    * Make the permalink work by setting post types when none are given
    */
    function parse_event_query( $query ) {
    $post_name = $query->get( 'name' );
    $post_type = $query->get( 'post_type' );
    if( ! empty( $post_name ) && empty( $post_type ) ) {
    view raw sluglesspermalink hosted with ❤ by GitHub

    The tough part to figure out. You notice that if you only did parts one and two, WordPress would give your new post type links but the links would 404. The reason? WP doesn’t know what post type to query for when it is given only a post name, like our permalink would resolve to. When all else fails, it’ll default to post and with that, won’t find our custom post.

    The solution uses the parse_query action to check all queries for two conditions: whether a name is being queried (ie name is not empty) and whether a post_type is set. If there is a name but not a post_type, the function will set post_type as post, page and our custom post type. Refresh your permalinks and the link should now work!

The only issue found with this is (thanks to Simon Blackbourn of wp-hackers) that if you happen to have a post or page of the same name, both will occupy the same page. Trashed posts of the same name will cause 404 errors until you delete it.

If you’ve found any flaws to my technique, please let me know. Feedback is appreciated!

3 thoughts on “Removing the Slug in a Custom Post Type Permalink

  1. This doesn’t work for me. I get a result of http://localhost/mydomain/%postname%/ for example. Any thoughts?

    • Manny Fleurmond

      Sorry, didn’t quite debug this thoroughly enough. Forgot to add a bit of code. The Gist now has the complete example and should be more or less bug free. Let me know if you run into any more problems: https://gist.github.com/funkedgeek/9233561

      • Hi,
        Thanks! I did get this working for the most part. It does not work only when the permalink is set to numeric. Not quite sure how to solve that one. Spent some time on it ,but I am not even close.

        Anyway, finally working code for removing slugs. THANK YOU!!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>