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:
-
Freshly installed WordPress in my local wamp server (enabled apache rewrite_module first).
-
Using default theme (twenty fourteen). No plugins installed.
-
Changed permalinks to “Post name”
-
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('jw_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:
-
Go to permalink, keep permalink settings to “Post name” and Save changes.
-
Use flush_rewrite_rules() in several places (one by one) in my code. Please see the commented out parts in the code above.
-
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.
Read more here: Custom Post Type - Permalink Problem