I added this script to my site to remove the slug for my CPT from my permalinks: https://wordpress.stackexchange.com/questions/212471/remove-slug-in-the-permalink-for-more-than-one-type-of-custom-post-type

And it works great!

The problem I have now is I activated the multisite functionality using subfolders and this script no longer works.

This URL used to work: http://example.com/single-post, but now goes straight to a 404 page and instead this is the post url: http://example.com/custom_post_type/single-post.

How can I adjust the functions so they return the permalink for this one specific CPT to http://example.com/single-post?

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

    if ( 'play_states' != $post->post_type || 'publish' != $post->post_status ) {
        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( 'post', 'play_states', 'page' ) );
    }
}
add_action( 'pre_get_posts', 'na_parse_request' );

Alternatively, this may be an .htaccess issue. Since the site is older than the allotted time for a subfolder multisite I had to force wordpress to accept it. Here is my current htaccess file:

# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]

# uploaded files
RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]

# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule  ^[_0-9a-zA-Z-]+/(wp-(content|admin|includes).*) $1 [L]
RewriteRule  ^[_0-9a-zA-Z-]+/(.*.php)$ $1 [L]
RewriteRule . index.php [L]

# END WordPress

Read more here: Remove slug from CPT permalink on a subfolder multisite (might be htaccess issue)


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: