• Resolved crazyred

    (@crazyred)


    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

    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/

    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/

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

    (@grapplerulrich)

    You could use 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

    (@crazyred)

    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

    (@crazyred)

    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

    (@crazyred)

    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

    (@crazyred)

    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 recommended by Ulrich, so again thanks a lot!

    Ulrich

    (@grapplerulrich)

    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

    (@crazyred)

    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.