0

I need remove slug movie from site.com/movie/mymoviename

I've tried remove 'slug' => 'movie', 'with_front' => FALSE,), from code, but this don't help's me. Here a code

    <?php 

/* Movies post type*/

function post_type_movies() {
register_post_type(
                    'movies', 
                    array( 'public' => true,
                            'publicly_queryable' => true,
                            'has_archive' => true, 
                            'hierarchical' => false,
                            'menu_icon' => get_stylesheet_directory_uri() . '/images/movie.png',
                            'labels'=>array(
                                        'name' => _x('Movies', 'post type general name'),
                                        'singular_name' => _x('Movie', 'post type singular name'),
                                        'add_new' => _x('Add New', 'Movies'),
                                        'add_new_item' => __('Add New Movie'),
                                        'edit_item' => __('Edit Movie'),
                                        'new_item' => __('New Movie'),
                                        'view_item' => __('View Movie'),
                                        'search_items' => __('Search Movies'),
                                        'not_found' =>  __('No Movies found'),
                                        'not_found_in_trash' => __('No Movie found in Trash'), 
                                        'parent_item_colon' => ''
                                        ),                           
                            'show_ui' => true,
                            'menu_position'=>5,
                            'query_var' => true,
                            'rewrite' => TRUE,
                            'rewrite' => array( 'slug' => 'movie', 'with_front' => FALSE,),
                            'register_meta_box_cb' => 'mytheme_add_box',
                            'supports' => array(
                                        'title',
                                        'thumbnail',
                                        'comments',
                                        'editor'
                                        )
                            ) 
                    );
                } 
add_action('init', 'post_type_movies');

/* Movie genre taxonomy */

function create_movie_genre_taxonomy() 
{
$labels = array(
                              'name' => _x( 'Movie Genre', 'taxonomy general name' ),
                              'singular_name' => _x( 'movie-genre', 'taxonomy singular name' ),
                              'search_items' =>  __( 'Search movie genres' ),
                              'all_items' => __( 'All movie genres' ),
                              'parent_item' => __( 'Parent movie genre' ),
                              'parent_item_colon' => __( 'Parent movie genre:' ),
                              'edit_item' => __( 'Edit movie genre' ), 
                              'update_item' => __( 'Update movie genre' ),
                              'add_new_item' => __( 'Add New Movie Genre' ),
                              'new_item_name' => __( 'New movie genre Name' ),
);  
register_taxonomy('movie-genre',array('movies'), array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'movie-genre' ),
  ));
}

add_action( 'init', 'create_movie_genre_taxonomy', 0 );

?> 

I've readed many posts about that, tried many plugins with "slug remover" but nothing help me. Maybe you can look in my code and say what's wrong.

1

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( '/' . $post->post_type . '/', '/', $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' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.