Title: Remove CPT slug while using polylang
Last modified: August 31, 2016

---

# Remove CPT slug while using polylang

 *  Resolved [crazyred](https://wordpress.org/support/users/crazyred/)
 * (@crazyred)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/remove-cpt-slug-while-using-polylang/)
 * Hi guys, I got an issue that I don’t know how to solve
 * Permalink is set to name of the post
    Polylang is set to use pretty permalink
   domain.com/fr/..
 * I created a custom post type “artiste”
    I published a post and it translated 
   versions Then I used the Ulrich polylang-slug plugin to use same unique post 
   slugs [https://github.com/grappler/polylang-slug](https://github.com/grappler/polylang-slug)
 * So I get the following urls for each post:
 * domain.com/fr/artiste/the-artist-name
    domain.com/en/artiste/the-artist-name 
   domain.com/../artiste/the-artist-name
 * Until now all works fine.
 * Then I wanted to remove category slug with the piece of code from
    [http://kellenmace.com/remove-custom-post-type-slug-from-permalinks/](http://kellenmace.com/remove-custom-post-type-slug-from-permalinks/)
 * The results expected is :
    domain.com/fr/the-artist-name domain.com/en/the-artist-
   name …
 * this is what permalinks looks like when i save post, and its working when I used
   it whithout polylang, but once polylang is active and set to pretty permalinks
   structure, then I get 404 when I browse it, while the url including category 
   slug still works
 * Don’t know exactly where to look at from this point, so if you guys could give
   me some direction it would be really appreciated 🙂
 * Thanks a lot!
 * [https://wordpress.org/plugins/polylang/](https://wordpress.org/plugins/polylang/)

Viewing 7 replies - 1 through 7 (of 7 total)

 *  [Ulrich](https://wordpress.org/support/users/grapplerulrich/)
 * (@grapplerulrich)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/remove-cpt-slug-while-using-polylang/#post-7246714)
 * You could use [Query Monitor](https://wordpress.org/plugins/query-monitor/) to
   help analyse the request. I may have a look if I can find a solution when I get
   some time.
 *  Thread Starter [crazyred](https://wordpress.org/support/users/crazyred/)
 * (@crazyred)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/remove-cpt-slug-while-using-polylang/#post-7246778)
 * Hey Ulrich,
 * Thanks a lot for your attention, for your great works and for the the tips.
 * I’m gonna try it right now.
 * I’ll keep the thread updated. Thanks again.
 *  Thread Starter [crazyred](https://wordpress.org/support/users/crazyred/)
 * (@crazyred)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/remove-cpt-slug-while-using-polylang/#post-7246784)
 * While looking around, I figured out that the parse request does not match and
   pass the argument post_type=”artiste”, instead it leave it as a “post”.
 *     ```
       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', 'artiste'));
           }
       }
   
       add_action('pre_get_posts', 'gp_parse_request_trick');
       ```
   
 * So with following requests I get this results :
 * domain.com/fr/artiste/the-artist-name -> OK
    domain.com/fr/the-artist-name?post_type
   =”artiste” -> OK domain.com/fr/the-artist-name -> 404
 * Querying by “name” still works fine with pages and posts even with identical 
   slug, and i reach the right url, but with custom post type it doesn’t do the 
   trick.
 *  Thread Starter [crazyred](https://wordpress.org/support/users/crazyred/)
 * (@crazyred)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/remove-cpt-slug-while-using-polylang/#post-7246794)
 * Another thing,
 * While polylang is set to “Hide URL language information on default language”,
   
   then default language url will looks like: domain.com/the-artist-name , and the
   parse trick just works fine
 *  Thread Starter [crazyred](https://wordpress.org/support/users/crazyred/)
 * (@crazyred)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/remove-cpt-slug-while-using-polylang/#post-7246800)
 * I got it working by changing the following condition :
 *     ```
       if (2 != count($query->query) || !isset($query->query['page'])) {
               return;
           }
       ```
   
 * to
 *     ```
       if (isset($query->query['lang'])) {
               if (3 != count($query->query) || !isset($query->query['page'])) {
                   return;
               }
           } else {
               if (2 != count($query->query) || !isset($query->query['page'])) {
                   return;
               }
           }
       ```
   
 * This way the query string can be filled by 2 arguments when URL contains language
   information :
    name & lang
 * While this is working, I wish wp’s gurus could give me an opinion about if its
   a right way to do.
 * That has been possible with use of [Query Monitor](https://wordpress.org/plugins/query-monitor/)
   recommended by Ulrich, so again thanks a lot!
 *  [Ulrich](https://wordpress.org/support/users/grapplerulrich/)
 * (@grapplerulrich)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/remove-cpt-slug-while-using-polylang/#post-7246819)
 * Ah, nice, I am not sure why the code is checking for the number of queries.
 * I was able to simplify the code to this and it works.
 *     ```
       function gp_parse_request_trick( $query ) {
   
       	// Only noop the main query
       	// Only noop our very specific rewrite rule match
       	// 'name' will be set if post permalinks are just post_name, otherwise the page rule will match
       	if ( ! $query->is_main_query() || ! isset( $query->query['page'] ) || empty( $query->query['name'] ) ) {
       		return;
       	}
   
       	$query->set( 'post_type', array( 'post', 'page', 'product' ) );
   
       }
       add_action( 'pre_get_posts', 'gp_parse_request_trick' );
       ```
   
 *  Thread Starter [crazyred](https://wordpress.org/support/users/crazyred/)
 * (@crazyred)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/remove-cpt-slug-while-using-polylang/#post-7246830)
 * Yes, my first try was the exact same approach and it works well, then because
   I was not sure about the queries counting check I just made an extra condition
   for this specific case.
 * The extra `empty( $query->query['name'] )` does make sens thought.
 * Since it works I’m gonna go with this one till I get to know if counting post
   does make sens as well.
 * Thanks! 🙂

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Remove CPT slug while using polylang’ is closed to new replies.

 * ![](https://ps.w.org/polylang/assets/icon-256x256.png?rev=3433336)
 * [Polylang](https://wordpress.org/plugins/polylang/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/polylang/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/polylang/)
 * [Active Topics](https://wordpress.org/support/plugin/polylang/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/polylang/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/polylang/reviews/)

## Tags

 * [slug](https://wordpress.org/support/topic-tag/slug/)

 * 7 replies
 * 2 participants
 * Last reply from: [crazyred](https://wordpress.org/support/users/crazyred/)
 * Last activity: [10 years, 1 month ago](https://wordpress.org/support/topic/remove-cpt-slug-while-using-polylang/#post-7246830)
 * Status: resolved