Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am having a hard time implementing a custom post type correctly. I have searched for a solution for a long time but couldn't find any. Here's what I did:

  1. Freshly installed WordPress in my local wamp server (enabled apache rewrite_module first).

  2. Using default theme (twenty fourteen). No plugins installed.

  3. Changed permalinks to "Post name"

  4. In the plugins folder, created a folder named pr_custom_posts and inside that, created a file named pr_custom_posts.php. In the file I created a custom post type. The code is as follows:

    /*
    * Plugin Name: PR Movies Custom Post
    * Plugin URI: http://www.example.com
    * Description: Learning Custom Posts
    * Author: PR
    * Author URI: http://www.example.com
    * Version: 1.0
    */
    
    class PR_Movies_Custom_Post {
    
    public function __construct() {
        $this->register_post_type();
        //flush_rewrite_rules();
    }
    
    public function register_post_type () {
        $args = array(
            'labels' => array (
                'name' => 'Movies',
                'singular_name' => 'Movie',
                'add_new' => 'Add New Movie',
                'add_new_item' => 'Add New Movie',
                'edit_item' => 'Edit Movie',
                'new_item' => 'Add New Movie',
                'view_item' => 'View Movie',
                'search_items' => 'Search Movies',
                'not_found' => 'No Movies Found',
                'not_found_in_trash' => 'No Movies Found in Trash'
            ),
            'query_var' => 'movies',
            'rewrite' => array (
                'slug' => 'movies/',
                'with_front'=> false
            ),
            'public' => true,
            'publicly_queryable' => true,
            'has_archive' => true,
            'menu_position' => 10,
            'menu_icon' => admin_url().'/images/media-button-video.gif',
            'supports' => array (
                'title',
                'thumbnail',
                'editor'
            )
        );
        register_post_type('pr_movie', $args);
        //flush_rewrite_rules();
    }
    }
    
     add_action('init', function() {
    new PR_Movies_Custom_Post();
    //flush_rewrite_rules();
     });
    

The Good Thing: The CPT is showing in my admin panel and I can add and view movies there.

THE PROBLEM: I cannot preview the movies in the front end (By clicking the "view" in the CPT in admin panel). It shows in the front end only when I set permalink to default (http://localhost/wp02/?p=123).

What I have tried:

  1. Go to permalink, keep permalink settings to "Post name" and Save changes.

  2. Use flush_rewrite_rules() in several places (one by one) in my code. Please see the commented out parts in the code above.

  3. Created a menu item as: URL: http://localhost/wp02/movies Navigation Label: Movies This creates a menu item in the front end but shows "Not Found" when "Movies" link is clicked.

This is driving me crazy. Can anyone please help on this? I would really appreciate.

share|improve this question
1  
Why you've added / in rewrite slug, replace that slash after movies and reflush the rewrite rules and you are good to go. –  Anjum Jun 29 at 11:34
    
@Anjum Thanks for your perfect solution. I was following a tutorial where / was added in the rewrite slug. Thanks again. –  user2407971 Jun 30 at 8:08
    
@Anjum please post that as an answer, so it can be properly accepted and visible as such :) –  Rarst Jun 30 at 8:57

1 Answer 1

Thanks to Anjum for his suggestion in the comments. The problem has been solved by removing the front-slash in the rewrite slug. The error was caused by:

'rewrite' => array (
    'slug' => 'movies/',
    'with_front'=> false
),

The error has been solved by the following modification:

'rewrite' => array (
    'slug' => 'movies',
    'with_front'=> false
),
share|improve this answer
    
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post. –  ialocin Jun 30 at 8:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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