I have a website with one custom post type as well as a blog.
For the custom post type I would like to have the slug as root, like so
http://domain.com/<title>
And the blog post I would like to display under /blog, like so
http://domain.com/blog/<title>
I've managed to "minimize" the custom post type's urls by using these two functions:
add_filter( 'post_type_link', function($post_link, $post, $leavename) {
if('custom' == $post->post_type && 'publish' == $post->post_status) {
$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link);
return $post_link;
}
return $post_link;
}, 10, 3 );
and
add_action('pre_get_posts', function($query) {
if ( ! $query->is_main_query() || 3 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
return;
}
if ( ! empty( $query->query['name'] ) ) {
$query->set( 'post_type', array( 'post', 'custom', 'page' ) );
}
});
However, I have not managed to move my blog posts to /blog.
I've tried to set the permalink structure to
/blog/%postname%/
and the custom post type's rewrite to
'rewrite' => array('slug' => 'custom', 'with_front' => false)
However, this does not produce the desired effect (domain.com/title). Instead, the "custom" slug is now part of the url again. Manually removing it (domain.com/title) produces a 404.
pre_get_posts
function? Especially the one with3 != ...
?3 !=
part I don't understand myself.