Remove Custom Post Type Slug from Permalink

Feb 12, 2014 by Mark Ward in Quick Tips, Web Design, WordPress

There are times that you want to stray from the regular permalink structure that WordPress applies to your custom post types.  For a recent project I was needing to remove the CPT slug from the permalink, so a page url like  “/events/some-event-slug/” would have the permalink of “/some-event-slug/.”  At first glance this doesn’t seem to be that difficult to accomplish, but then you start looking through the WordPress.org forums for a solution and come back with several different “solutions” and none of which work for you. Then there are time you find a script that just works, and that is what I am sharing with you.

Thanks WordPress VIP for posting a solution.

Just paste the following code in your functions.php file and replace “your_post_type” with the slug of the post type you wish to drop the slug from.

When that’s done be sure and update permalinks. That’s it.

See original post here:  http://vip.wordpress.com/documentation/remove-the-slug-from-your-custom-post-type-permalinks/

 

Tags:

  • Jacob Perl

    Doesn’t work for me…wonder if it could because my CPT is created with Toolset Types?

    • markwarddesign

      I don’t know if that would make a difference. I could take a look for you if you like.

    • Vasily Ogar

      because at the line 6 you need to change ‘event’ on your post type

    • theatereleven

      This didn’t work for my CPT either….bummer man.

    • Serhii Prokhorenko

      Hi man! Maybe you could share the solution for Types if found one?

  • theatereleven

    This worked for me – replace the two booyahs with your CPT:

    /**

    * Remove the slug from the OGs CPT

    */

    function vipx_remove_cpt_slug( $post_link, $post, $leavename ) {

    if ( ! in_array( $post->post_type, array( ‘booyah’ ) )

    || ‘publish’ != $post->post_status )

    return $post_link;

    $post_link = str_replace( ‘/’ . $post->post_type . ‘/’, ‘/’, $post_link );

    return $post_link;

    }

    add_filter( ‘post_type_link’, ‘vipx_remove_cpt_slug’, 10, 3 );

    /**

    * Some hackery to have WordPress match postname to any of our public

    * post types. All of our public post types can have /post-name/ as

    * the slug, so they better be unique across all posts. Typically core

    * only accounts for posts and pages where the slug is /post-name/

    */

    function vipx_parse_request_tricksy( $query ) {

    // Only noop the main query

    if ( ! $query->is_main_query() )

    return;

    // Only noop our very specific rewrite rule match

    if ( 2 != count( $query->query )

    || ! isset( $query->query[‘page’] ) )

    return;

    // ‘name’ will be set if post permalinks are just post_name,

    // otherwise the page rule will match

    if ( ! empty( $query->query[‘name’] ) )

    $query->set( ‘post_type’, array( ‘post’, ‘booyah’, ‘page’ ) );

    }

    add_action( ‘pre_get_posts’, ‘vipx_parse_request_tricksy’ );

    • Irvin

      this 1 worked for me but the problem is that it gives me 404 for pages like:

      domain.com/parent/child

      any modifications needed?

      • Perhaps you have a conflict with another page/post with the same slug as your custom post type?

      • markwarddesign

        Maybe there is a conflict with another page or post slug?

        Is the page for your custom post type a custom page with a query or an archive page?

  • Rob Healey

    Thankyou, I have got the result I’ve been searching for,
    but this code doesn’t work for me on its own.

    Luckily I had a plugin activated that also didn’t work on it’s own to do this job, but combined code + plugin and Voila, I have mydomain.com/postname
    Kinda weird and I worry when things like this happen – it’s not right and I don’t trust it.
    Any ideas why this worked?
    Plugin is Remove slug from custom post type
    It puts this code into .htaccess
    # BEGIN REMOVE SLUG CUSTOM POST TYPE RULES
    RewriteRule ^acf/(.+)/$ /$1 [R=301,L]
    RewriteRule ^cycloneslider/(.+)/$ /$1 [R=301,L]
    RewriteRule ^faqs/(.+)/$ /$1 [R=301,L]
    RewriteRule ^media/(.+)/$ /$1 [R=301,L]
    RewriteRule ^procedures/(.+)/$ /$1 [R=301,L]
    RewriteRule ^resources/(.+)/$ /$1 [R=301,L]
    RewriteRule ^before_and_after/(.+)/$ /$1 [R=301,L]
    # END REMOVE SLUG CUSTOM POST TYPE RULES
    faqs,media,procedures,resources and before and after are custom post types names.

  • Hi MarkWardDesign,
    Thanks for sharing… On the first attempt this didnt work for me and I noticed it is because there is another instance of the post type included that has to be changed… On line 28 you include: ‘your_post_type’, but we should also include that in line 6 to replace the reference to ‘event’ in there which I think you meant to also be ‘your_post_type’.

    That almost worked but now I am getting 404’s when I try to view my custom post type… perhaps this is a WP 3.9.1 thing because I had this working before…?

    Anyone had success?

  • Perfect ! Works great
    Don’t forget to change the value on line 6 ans on line 28 by your own values ! Save the permalinks and this is it ! thanks a lot !

    • markwarddesign

      Approve—
      Sent from Mailbox

  • Pingback: remove base slug from hierarchical custom post type()

  • Pingback: remove base slug from hierarchical custom post type - HelpDesk()

  • Pingback: remove base slug from hierarchical custom post type | Question and Answer()

  • Jake

    Mark, you are a lifesaver. For some reason an old plugin I was using stopped working and I was scrambling to find a fix. Your code here did the trick. Many thanks!!

  • Domenic

    Works for me. How does this affect performance?

  • TheNightFox

    Can anyone tell me how to edit the code so that I can remove multiple custom post type slugs?

  • This thing just saved my life! Thank you!

  • Nik Tsupa

    Mark , THANK YOU!!!

  • Beauty

    Hi, my post type name is ‘job’ Do I change one thing and one thing only ‘your_post_type’ to ‘job’ in line 28? It doesn’t work. I add this in functions.php

  • Amanda

    I have some questions/concerns. This code works but results in a 404 – which I’ve run into with every attempt to get rid of the CPT slug. It seems inevitable, because WordPress no longer recognizes the Post as a Post. The way to get around this is with some redirect rules but this is a hack and not good for SEO.

    But I’ve found that that slug being in the URL structure is also bad for SEO – that slug does not create a hub page or archive that you can navigate to. Am I right? So it’s just adding an extra layer to your URL and if you want anything other than /cpt-slug/post-title (say for example /cpt-slug/category/post-title you are going three levels deep which is bad for SEO rankings. Not the end of the world, but also not ideal. It also doesn’t make sense from a users standpoint – shouldn’t /cpt-slug/ be something you can navigate to?

    I’m in a pickle here, because I have content and the only way that makes sense to add/organize it is through CPT. But that darn slug is ruining everything! Is there a clean/non-hack way to solve this? Maybe not using CPT at all but something else? I don’t see that as an option but I am still learning a lot about WordPress.

  • Worked great! Thank you for the customization!

  • dharmesh

    Thanks a lot for giving solution but in my case I have URL like this http://example.org/summer-camp/computer/
    When I click on summer-camp link its working fine but When click on computer link it showing me 404 page not found error..It means it worked on parent page but not on child page…any suggestion?? Please help me to short out this problem….Thanks in advance

  • Aamir Hussain

    This does not work for child pages.