WordPress remove post type base for one post

I’m trying to remove post type base for one selected post. For now I’ve got:

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

if ( 'oferta' != $post->post_type && $post->ID != '42') {
    return $post_link;
}

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

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

function na_parse_request( $query ) {

if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
    return;
}

if ( ! empty( $query->query['name'] ) ) {
    $query->set( 'post_type', array( 'oferta' ) );
}
}
add_action( 'pre_get_posts', 'na_parse_request' );

And my link changed from http://example.com/cat/single-post to http://example.com/single-post but I’ve got 404 error. Any ideas how to fix it?

Related posts

Leave a Reply