If you create a custom post type in WordPress, its slug will be added to the permalink (URL) structure by default. This can be desirable for grouping types of posts together, but it can also result in longer, harder-to-remember URLs, not to mention articles that don’t rank as highly on Google  according to Yoast, who is an authority on WordPress search engine optimization (SEO). In this post, I’ll cover how to safety and properly remove the custom post type slug from your permalinks.

Recently, I began creating a number of sites for a racing company that puts on 5K, 10K, marathon, and other distance races and fun runs. For these sites, I created a custom post type called “race”. This resulted in permalinks such as example.com/race/race-title. As you can see, this is not nearly as clean, easy to remember, or search engine optimized as having permalinks like example.com/race-title would be. I found a great post on WordPress.com VIP entitled “How to Remove Custom Post Type Slug from Permalinks,” but it appears that the post has been deleted, so I’ll outline the process below.

1. Create your custom post type (unless already created). This can be done very easily by using the great, free Custom Post Type UI plugin.

2. Create a plugin for our new code to live in (yes, it could go in the functions.php file, but then it’d be lost if the theme were changed!).

3. Filter the permalink for our custom post type such that all published posts don’t have the slug in the URI:

4. At this point, trying to view the link would result in a 404. That’s because WordPress only knows the URI can be Posts or Pages. We need to teach it to also pay attention to our custom post type by adding the following:

5. That’s it! Just change both instances of “race” to the slug of your custom post type, and replace “gp_” with whatever function prefix you’d like (your initials would be fine), and you should be all set. Going to Settings > Permalinks and saving the permalink structure to end in /%postname%/ may also be necessary.

65 comments

  1. Коля Ципа January 15, 2015 at 12:34 pm

    Reply

    THANK YOU!!!

    • Kellen Mace February 23, 2015 at 11:03 pm

      Reply

      You’re very welcome 🙂

  2. Jennifer February 22, 2015 at 5:33 am

    Reply

    what do you mean by replacing gp_ with whatever functions? Can you give me an example of all codes for my post type ‘job’?

    • Kellen Mace February 23, 2015 at 11:02 pm

      Reply

      Jennifer – in WordPress, it’s possible that if two functions have the same name, they could conflict with each other on your site and cause errors. To prevent that, adding a function prefix to the beginning of the function names is recommended. So in the example below, I’ll use your initials (jl) followed by an underscore (_) for the function prefixes and use ‘job’ for your custom post type slug:

      /**
      * Remove the slug from published post permalinks. Only affect our CPT though.
      */
      function jl_remove_cpt_slug( $post_link, $post, $leavename ) {

      if ( ‘job’ != $post->post_type || ‘publish’ != $post->post_status ) {
      return $post_link;
      }

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

      return $post_link;
      }
      add_filter( ‘post_type_link’, ‘jl_remove_cpt_slug’, 10, 3 );

      /**
      * Have WordPress match postname to any of our public post types (page, post, race)
      * All of our public post types can have /post-name/ as the slug, so they better be unique across all posts
      * By default, core only accounts for posts and pages where the slug is /post-name/
      */
      function jl_parse_request_trick( $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’, ‘job’, ‘page’ ) );
      }
      }
      add_action( ‘pre_get_posts’, ‘jl_parse_request_trick’ );

      I hope that helps. Let me know if you have any more questions.

  3. Mark Kelly February 24, 2015 at 9:33 am

    Reply

    I’m having an issue using this code on a WP multisite main site.

    By default WP Multisite adds /blog/ to the base URL of any post type.

    So say I have a cpt with slug ‘city-pages’. By default my URL will look like example.com/blog/city-pages/dublin

    Running your script in my functions file will rename that URL to example.com/blog/dublin. This URL will work and be browse-able.

    Then I want to get rid of the /blog/ part so one way to do that is by specifying ‘with_front = false’ on the CPT settings creation. That will leave me with the desired URL of example.com/dublin but browsing to that URL returns a 404.

    In your script do I have to account for the extra /blog/ url to make the rewrite work correctly?

    Any thoughts?

    • Kellen Mace March 1, 2015 at 8:27 pm

      Reply

      Mark,
      Sorry, I’m not all that familiar with the rewrite rules for multisite installs; I’m not sure why you’re getting a 404. If you can’t seem to get it working, maybe you could find a developer who specializes in multisite installs and can find a solution. Best of luck!

    • Justin September 11, 2015 at 6:43 pm

      Reply

      Hey Mark – did you ever find a solution for multisite? This code works great for a single install, but as you found awhile back, stops working with multisite. Any luck finding a solution?

      • Ash Bryant April 4, 2016 at 12:27 pm

        Reply

        Hi Justin,

        Did you get this working at all?

  4. Ginnie March 11, 2015 at 3:04 am

    Reply

    Thank you so much Kellen, you saved me a day!! 🙂

    • Kellen Mace March 11, 2015 at 6:29 pm

      Reply

      You’re very welcome 🙂

  5. Michelle March 12, 2015 at 3:34 pm

    Reply

    Thanks, this works really well when the permalink structure is just /%postname%/ but I need the structure to be /%category%/%postname%/ for a different part of the website but then I get 404 errors for the custom type post.

    Is there anyway to just remove the slug from the custom post type whilst still being able to use categories for another section?

    • Kellen Mace March 16, 2015 at 10:26 pm

      Reply

      Michelle,

      It sounds like you may need to add your custom post type to the tag & category pages, since WordPress does not automatically take newly created custom post types and add them to the archive pages for existing taxonomies. You can check out this WPMUDEV post on how to do it. Hope that helps out.

      • Amanda August 19, 2015 at 3:03 pm

        Reply

        Hi Kellen,

        Thank you for this! It has helped to remove my slug, but I am still getting a 404 for a similar reason to Michelle. I have my cpt set up with it’s own permalink structure: /cpt-slug/%cpt-taxonomy%/%postname%/. With your code my URLs are showing sans slug, but they are 404ing. So I added the code from that WPMUDEV post you linked to, and it is still 404ing. Would you have any advice on how to fix the 404 in my case?

      • TJ Kelly December 7, 2015 at 1:49 pm

        Reply

        Hi Kellen (and Michelle and Amanda), I have the same exact question as Michelle above. My permalink structure is “/%category%/%postname%/”

        After implementing your code, my modified CPT URLs are 404ing. When I change the permalink structure to “/%postname%/” they work great.

        I added the WPMUDev code you suggested in response to Michelle, but it didn’t help. I also don’t think it answered her/my question. I’m not interested in a category archive of my CPT posts, a la movies.tld/category/drama. I just want to strip out the CPT slug:

        mysite.com/cpt/post-title-here/ should become mysite.com/post-title-here/

        Can you offer any advice on how to modify your gp_remove_cpt_slug() or gp_parse_request_trick() functions to accomplish this?

        • Vara February 12, 2016 at 2:04 am

          Reply

          Hi Kelly, Did you get any solution? If you solved that can you please share that with me.

          Thanks.

      • Amy June 29, 2016 at 5:22 pm

        Reply

        I realize this is over a year old, but wanted to comment that this method doesn’t seem to work if the Permalink is anything except ‘postname’. I don’t think the other commenterss issue was that they need to add tags and categories to the custom post type… it’s just that when they set the Permalink that way, the custom post type returns 404s or the post type slug is not removed… it varied on my tests from post to post. My particular permalink was set to a date based… when I switched it to postname, it worked… but the client prefers a date based permalink for their regular posts.

  6. Charles March 24, 2015 at 9:03 pm

    Reply

    Thanks very much for illustrating how to do this. Very helpful.

    One thing I encountered was that I grabbed an array of all my custom post types and then just used the += operator to add post and page to the end of that array in _parse_request_trick(). However, the blog posts started returning 404 when attempting to visit their public view. For some reason, WP needs post to be the first element in the array passed as the second param to $query->set() method. I put page on the end of the array since that’s how it is in your example. Was wondering why you had the custom post type in the middle.

    Mine now looks like this:


    $post_types = array_keys(pixo_get_list_of_custom_post_types());
    array_unshift($post_types, 'post');
    array_push($post_types, 'page');
    if ( ! empty( $query->query['name'] ) ) {
    $query->set( 'post_type', $post_types );
    }

  7. Ronen Schmitz April 13, 2015 at 2:30 am

    Reply

    Sorry, all fine now. Please delete my comment above. The code from github is now visible and I found my error!
    Thanks again, working wonderfully now.

    • Kellen Mace April 20, 2015 at 7:14 pm

      Reply

      Ronen – no problem! Glad you got it working.

  8. irfan April 30, 2015 at 2:10 pm

    Reply

    Thank you so much you save my time 🙂

  9. Bhuvnesh May 1, 2015 at 8:41 am

    Reply

    I am using two custom post types. abc-1 and abc-2 . By using your code only one custom post type is working fine second one is redirecting to 404.

    Please help.

    • Kellen Mace June 23, 2015 at 9:39 pm

      Reply

      Bhuvnesh,

      To remove the slug for multiple custom post types, you can change lines 7-9 of the first function to this:


      $custom_post_types = array( 'book', 'movie', 'tv_show' );
      if ( ! in_array( $post->post_type, $custom_post_types ) || 'publish' != $post->post_status ) {
      return $post_link;
      }

      And change line 19 of the second function to:


      $query->set( 'post_type', array( 'post', 'page', 'book', 'movie', 'tv_show' ) );

      Just replace ‘book’, ‘movie’, and ‘tv_show’ with whatever custom post types you want to remove the CPT slug from.

  10. Steve Davies May 15, 2015 at 8:16 am

    Reply

    This works great – however i’ve got posts that have children but it seems to break…

    If my CPT is /bookz/, a normal post URL looks like /bookz/test/ – and this rewrites correctly to /test/.

    However, if this post has a child post /test/debug/, I get a 404. Going to it’s FQDN /bookz/test/debug loads the page…

    Can you tell me how to make this work for child posts too?

    (I’ve tried this and it rewrites the URL, but visiting it doesn’t work as described above)

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

    if ( 'bookz' != $post->post_type || 'publish' != $post->post_status ) {
    return $post_link;
    }

    $parts = explode('/', $post_link);
    unset($parts[3]);
    $post_link = implode('/', $parts);

    return $post_link;
    }
    add_filter( 'post_type_link', 'gp_remove_cpt_slug', 10, 3 );

    • Kellen Mace June 23, 2015 at 9:19 pm

      Reply

      Steve,

      Sorry for the delay. If you’re still looking for an answer to this, please see Vlado’s solution in this comment thread for child posts.

    • just a guy April 10, 2016 at 1:34 pm

      Reply

      Hey man this does not work, the top code works fine but I am having trouble doing this for multiple post types maybe you was not clear enough exactly what lines to change as you say lines 7-9 in the first function then line 19 in second function I do this change to my post types and get white screen until I undo the changes?

      • Kellen Mace April 25, 2016 at 10:43 pm

        Reply

        The code line numbers have changed a little bit since I left that comment, so here are updated instructions. If you want to remove the permalink slug for the custom post types ‘book’, ‘movie’ and ‘tv_show’, you would replace lines 8-10 of the first function with this:

        $custom_post_types = array( 'book', 'movie', 'tv_show' );
        if ( ! in_array( $post->post_type, $custom_post_types ) || 'publish' != $post->post_status ) {
        return $post_link;
        }

        …and replace line 21 in the second function with this:

        $query->set( 'post_type', array( 'post', 'page', 'book', 'movie', 'tv_show' ) );

  11. Ryan May 19, 2015 at 6:50 pm

    Reply

    Great work! THANK YOU!

  12. Bnimbhal June 4, 2015 at 1:21 am

    Reply

    Hello,

    Plz guide for the child theme.

    Thanks.

    • Kellen Mace June 23, 2015 at 9:25 pm

      Reply

      Bnimbhal,

      The code samples above should work for either a parent or child theme. If you want to get it to work for child posts of your custom post type, please see Vlado’s solution in this comment thread.

  13. Vlado June 23, 2015 at 10:10 am

    Reply

    Hy,

    Thanks for this, it works, but not for subpages of custom post type.
    Any hint?

    Thanks in advanced

    • Vlado June 23, 2015 at 10:28 am

      Reply

      Let me reply to my own question.
      Thing is if you have a child page, then you’r post is accessible on ‘site.com/child-page’, but links generated are like: ‘site.com/parent-page/child-page’ which returns 404.

      I have added one more if to your function so now it looks like:


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

      if ( 'location' != $post->post_type || 'publish' != $post->post_status ) {
      return $post_link;
      }
      // This part is new
      if( $post->post_parent ) {
      $parent = get_post($post->post_parent);
      $post_link = str_replace( '/' . $post->post_type . '/' . $parent->post_name . '/', '/', $post_link );
      }
      else {
      $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
      }

      return $post_link;
      }
      add_filter( 'post_type_link', 'gp_remove_cpt_slug', 10, 3 );

      • Kellen Mace June 23, 2015 at 9:17 pm

        Reply

        Vlado,

        I’m glad you got it working for child pages, and thanks for posting your solution so others can benefit from it, too 🙂

      • Colir November 10, 2015 at 11:16 am

        Reply

        Hi. Thanks for this solution with child page.
        However, i cant’ get a solution to kept the parent post name in the url
        ex: http://www.domain.com/cpt-parent-page/cpt-child-page

        any idea ?
        thanks

  14. BOUM July 7, 2015 at 8:04 am

    Reply

    Hello !
    Very nice work @Kellen Mace !!! It works fine
    But I have a question related to this topic. How do I remove the slugs of my post type into my breadcrumb ?
    My permalink is ok with your work (http://my-website.com/connexion/), unfortunately my breadcrumb show : “Home > My Slug > Connexion”.
    Have you got an idea to do that ?

    Thank you so much !

    • BOUM July 7, 2015 at 9:25 am

      Reply

      After some searches I have found the solution. So I share it with you (maybe someone will be glad to found it). Add this in your custom breadcrumb code or in the native one in your functions.php file :
      }

      elseif ( get_post_type() == 'log' ) {

      echo '' ;

      echo '';

      if ( $showCurrent == 1 ) {
      echo '' . get_the_title() . '';
      }

      Warning, dont forget to close the ‘elseif’ attribute if you are coding your own breadcrumb !
      Big kisses !!!

  15. Shashank Sharma July 20, 2015 at 2:47 am

    Reply

    It works great. But having issue with urls means both url is working like http://www.website.com/%post_type%/singlepost and http://www.website.com/singlepost. I do not want to show both urls only will show url like http://www.website.com/singlepost

  16. Amanda August 19, 2015 at 3:15 pm

    Reply

    Hi Kellen,

    This is great! It worked to remove my slug. But it is still 404ing – maybe because I have a custom taxonomy in the permalink? Right now my permalink structure is: /cpt-slug/%cpt-taxonomy%/%postname%/ and it is displaying on the front-end without the slug (yay!) but 404ing. Do you know how I can stop the 404ing?

  17. Regil August 25, 2015 at 3:17 am

    Reply

    Hello,

    thanks for this. this really works.

    I have a problem which is all my post-name have a .php extension when i use this code it removes the slug which i want but i get 404 . when i remove the .php it works fine. what will i do to make it work with .php?

    Thank you.

  18. David September 1, 2015 at 5:16 pm

    Reply

    Very nice work!
    There’s only one bug, if there are more same titles, usually the system makes -2, -3, in the titles.
    In this case the system doesn’t do this.
    Solutions?

    Thanks

  19. phil salt September 15, 2015 at 5:02 pm

    Reply

    Thank you Ellen.

    Had been looking for this solution for awhile. Solution works with WooCommerce once Product permalink base has been set to standard. Be well, phil

  20. jasbirsingh September 21, 2015 at 5:17 am

    Reply

    Exactly what i was looking for. Thank you 🙂

  21. Dave M September 26, 2015 at 9:14 am

    Reply

    I have been on this for HOURS! And I mean for HOURS! The overall problem has had me for days. I have been searching for a solution all night and my history has over 40 pages of material I have been looking at.

    And then I find your post. Thank you very much! If you had a donate button, you would have got a beer from me, think about adding one! 😀

    Cheers!

  22. George October 25, 2015 at 9:07 am

    Reply

    Great advice, thanks!

  23. dharmesh October 28, 2015 at 5:24 am

    Reply

    Thanks a lot for giving solution….But in my case url is like
    http://example.org/camp/summer-camp/computer
    http://example.org/camp/summer-camp/dashboard

    So In this case i want to remove slug as ‘camp’ and other thing will be same like
    http://example.org/summer-camp/computer
    http://example.org/summer-camp/dashboard

    For that i have applied first solution but it works on parent page like ‘summer-camp’ and not on child page ‘computer’ ….Any solution for this…Please help me to short out this problem…Thanks in advance.

  24. Pingback: Iurie Malai on "[Plugin: WPAdverts - Classifieds Plugin] Remove the "advert" slug or replace it with the adverts category name" * Best Wordpress Themes - Reviews

  25. Adaki November 17, 2015 at 5:56 am

    Reply

    Hi;

    My permalink structure is like this:
    /news/%postname%/

    How can modify this code to work?

    Thanks!

    • Kellen Mace November 17, 2015 at 7:44 am

      Reply

      Adaki – Use both of the code snippets in the article, replacing ‘race’ with ‘news’.

  26. roderik November 20, 2015 at 10:38 am

    Reply

    Hi Kellen,

    Do you know how I can fix the issue that after this code I can reach the custom post with both urls?
    domain.com/custom-post/custom-post-single
    and
    domain.com/custom-post-single.

    This will give a lot of duplicate content.

    Would love to know if there is a solution for it.

    With kind regards,

    Roderik

    • Kellen Mace November 20, 2015 at 3:43 pm

      Reply

      Roderick,

      The easiest thing to do would be to set up a redirect so that requests for domain.com/custom-post/custom-post-single are redirected to domain.com/custom-post-single. Here’s how to do that:

      1. Install and activate the Safe Redirect Manager plugin from wordpress.org.
      2. Go to Tools > Safe Redirect Manager and create a new rule. For Redirect From, enter /custom-post/* and for Redirect To, enter /*

      I hope that helps out!

  27. mind n December 9, 2015 at 2:13 am

    Reply

    Thanks you, Kellen!

    You helped me a lot

  28. Bayzid December 21, 2015 at 5:35 am

    Reply

    I have used this code for my custom post type “resource”. This removed slug from url but when i click on link it does not go to the page. Can you help me to find the error. My post page is here https://www.paramountplus.com.au/resources/
    /**
    * Remove the slug from published post permalinks. Only affect our custom post type, though.
    */
    function gp_remove_cpt_slug( $post_link, $post, $leavename ) {

    if ( ‘resource’ != $post->post_type || ‘publish’ != $post->post_status ) {
    return $post_link;
    }

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

    return $post_link;
    }
    add_filter( ‘post_type_link’, ‘gp_remove_cpt_slug’, 10, 3 );
    function gp_parse_request_trick( $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’, ‘page’, ‘resource’ ) );
    }
    }
    add_action( ‘pre_get_posts’, ‘gp_parse_request_trick’ );

  29. Tomás Barão December 26, 2015 at 10:49 pm

    Reply

    WordPress permalinks are kinda complicated, but you just saved me!
    Thankyou very much! 😀 😀 😀

  30. Chris January 11, 2016 at 3:36 pm

    Reply

    Thanks for this! It works great except that it is now causing my pages to act like posts. Now, when I create a page with a custom page template, the page template is being ignored and instead is loading what appears to be single.php as the template.

  31. Adrian February 24, 2016 at 10:22 am

    Reply

    Chris, THANK YOU!!! I’ve been searching for something like this for about 2 weeks. Very easy code. Never thought it’s possible to have a post/page/custom taxonomy with the same link. THANKS!!!

  32. Joseph April 4, 2016 at 9:41 pm

    Reply

    The function works for me, but I have been trying to modify code to allow me to show parent in URL of child a child post.
    I have rewritten the the url, so a child post points to example: domainhere.com/parent/child-post/
    but the url is a 404, and the post content is at domainhere.com/locations/parent/child-post/
    //My code///
    function jtr_remove_cpt_slug( $post_link, $post, $leavename ) {

    if ( ‘location’ != $post->post_type || ‘publish’ != $post->post_status ) {

    return $post_link;

    }

    // This part is new

    if( $post->post_parent ) {

    $parent = get_post($post->post_parent);

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

    }

    else {

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

    }

    return $post_link;

    }

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

    /**
    * Have WordPress match postname to any of our public post types (post, page, location)
    * All of our public post types can have /post-name/ as the slug, so they better be unique across all posts
    * By default, core only accounts for posts and pages where the slug is /post-name/
    */
    function jtr_parse_request_trick( $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’, ‘page’, ‘location’ ) );
    }
    }
    add_action( ‘pre_get_posts’, ‘jtr_parse_request_trick’ );

    //MYCODE//
    I am trying to keep the permalinks I setup for child posts, but have the content be there and not be at domainhere.com/locations/parent/child-post/

    I appreciate any help.

  33. Pingback: Remove custom post type slug but keep related category taxonomy permalink | Question and Answer

  34. Milan July 27, 2016 at 10:25 am

    Reply

    This solution works good. However, I want a structure for a CUSTOM POST TYPE with year, month and date: http://www.website.com/%year%/%monthnum%/%day%/%postname%/. Your solution works if I set the permalink structure to “post name”. When I make a custom structure like /%year%/%monthnum%/%day%/%postname%/, I get 404 error.

  35. No name August 1, 2016 at 12:18 am

    Reply

    Thaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaanks

  36. Kristian September 23, 2016 at 11:37 am

    Reply

    Anyone know how to rewrite permalink of custom post type?
    i’m facing on this problem i tried many thing but it’ doesn’t work

    • Kellen Mace September 23, 2016 at 3:21 pm

      Reply

      Hey Kristian,

      To rewrite the slug that appears in the URL for a custom post type, you need to pass in the ‘rewrite’ argument when you register the post type with register_post_type(). See the first example under the ‘User Contributed Notes’ section on this page for an example of how to do it: https://developer.wordpress.org/reference/functions/register_post_type/

      Best of luck!

  37. Juan September 26, 2016 at 3:55 pm

    Reply

    Hi, I applied the Vlado’s solution and it doesn’t work. It remove succesfully the slug but when I clicked in a link to a child page of the CPT shows me the 404 error. I paste the code here.
    Thanks in advance and sorry for my poor english.

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

    $custom_post_types = array( ‘doctorado’, ‘maestrias’, ‘programas’ );

    if ( ! in_array( $post->post_type, $custom_post_types ) || ‘publish’ != $post->post_status ) {

    return $post_link;

    }

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

    if ( ‘location’ != $post->post_type || ‘publish’ != $post->post_status ) {

    return $post_link;

    }

    // This part is new

    if( $post->post_parent ) {

    $parent = get_post($post->post_parent);

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

    }

    else {

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

    }

    return $post_link;

    }
    add_filter( ‘post_type_link’, ‘jp_remove_cpt_slug’, 10, 3 );

    /**
    * Have WordPress match postname to any of our public post types (post, page, race)
    * All of our public post types can have /post-name/ as the slug, so they better be unique across all posts
    * By default, core only accounts for posts and pages where the slug is /post-name/
    */
    function jp_parse_request_trick( $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( ‘page’, ‘post’, ‘doctorado’, ‘maestrias’, ‘programas’ ) );
    }
    }
    add_action( ‘pre_get_posts’, ‘jp_parse_request_trick’ );

  38. Artur Bruno October 3, 2016 at 10:46 am

    Reply

    This form allows you to create multiple posts with the same slug, as he chooses which reveal post as the slug is the same? I did some tests … There are 2 posts, each in a different post type, but both have the same slug, but only one is returned. Is there any way to add ‘2’ at the end of the slug as it is done by default in wordpress? So avoid that existed two posts or more with the same slug and solve the problem in complito URL. Sorry English, the message was translated from Portuguese

  39. Luigi Quesada October 7, 2016 at 7:07 pm

    Reply

    Hi, nice code, It works fine! Thank you.

  40. Shabeee November 14, 2016 at 7:06 am

    Reply

    Thank you!!!! Your help was like getting painkiller after being in terrible pain for days.

  41. Shabeee November 14, 2016 at 7:39 am

    Reply

    Darn it, the code works great in creating url in admin panel, but I get a 404 error when trying to view custom post pages. I see others are having same issue. Going to try and troubleshoot. Will post back with any working solutions or revelations.

    Thanks for providing great tips and an open forum via comments.

Leave a reply

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