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.
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.
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.
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
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!
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' );
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! 🙂