After doing some research I found following code, made it a plugin (you have to create a php file and give it a name,copy code below into that file and place it in your plugins folder).
It is not my code so therefore I put references to the author and original source-code and links to github.
You have to (re)set your permalinks after activating this plugin by going to:
Settings > Permalinks in the admin panel and click Save Changes
NOTE:
Your function which creates the cpt is imho not 100% correct (ie. using twice 'rewrite'=>
, and using a text-domain or don’t but rather do not use it partly as you do),
maybe re-creating it is a better option? Online engine to create a CPT
Just a hint, when using custom post type’s make it a plugin and put them in your plugins folder. (So after switching theme’s(ie. to test) you still have your custom post at hand.
I tested it not local but they say it works
I can not guarantee that there won’t be issues with it in the near future because it is(at least seem to me) rare to leave slugs out the way you want.
<?php
/*
Plugin Name: Rewrite CPT Movie slug
Description: Removes slug from published post type permalinks.(Only affect our CPT though) / Have WordPress match postname to any of our public post types (movie, page, post)
Author: Charles
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
/**
* Remove the slug from published post permalinks. Only affect our CPT though.
*
* @author Kellen Mace <http://kellenmace.com>
* @link http://kellenmace.com/remove-custom-post-type-slug-from-permalinks/
* @see https://gist.github.com/kellenmace/65d100fa6c76d249c53f#file-remove-custom-post-type-slug-from-permalinks-2-php
*/
function remove_cpt_slug_191875( $post_link, $post, $leavename ) {
if ( 'movie' != $post->post_type || 'publish' != $post->post_status ) {
return $post_link;
}
$post_link = str_replace( "https://wordpress.stackexchange.com/" . $post->post_type . "https://wordpress.stackexchange.com/", "https://wordpress.stackexchange.com/", $post_link );
return $post_link;
}
add_filter( 'post_type_link', 'remove_cpt_slug_191875', 10, 3 );
/**
* Have WordPress match postname to any of our public post types (movie, page, post)
* All of our public post types can have /post-name/ as the slug, so they better be unique across all posts
* By default, core only accounts for posts and pages where the slug is /post-name/
*
* @author Kellen Mace <http://kellenmace.com>
* @link http://kellenmace.com/remove-custom-post-type-slug-from-permalinks/
* @see https://gist.github.com/kellenmace/b39553b3c7243ff62040#file-remove-slug-from-custom-post-type-php
*/
function parse_request_trick_191875( $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( 'movie', 'post', 'page' ) );
}
}
add_action( 'pre_get_posts', 'parse_request_trick_191875' );