23

I made a custom post type with the machine name special_media_post and wordpress is simply not seeing the single-special_media_post.php. I am at a complete lose. It keeps defaulting to the index.php

Here is my code for my custom post type and its taxonomies:

//Post and Taxonomy stuff
//Register Custom Post Type
function special_media_post() {
$labels = array(
    'name'                => _x( 'Media Posts', 'Post Type General Name', 'text_domain' ),
    'singular_name'       => _x( 'Media Post', 'Post Type Singular Name', 'text_domain' ),
    'menu_name'           => __( 'Media Post', 'text_domain' ),
    'parent_item_colon'   => __( 'Media Post:', 'text_domain' ),
    'all_items'           => __( 'All Media Posts', 'text_domain' ),
    'view_item'           => __( 'View Media Post', 'text_domain' ),
    'add_new_item'        => __( 'Add New Media Post', 'text_domain' ),
    'add_new'             => __( 'New Media Post', 'text_domain' ),
    'edit_item'           => __( 'Edit Media Post', 'text_domain' ),
    'update_item'         => __( 'Update Media Post', 'text_domain' ),
    'search_items'        => __( 'Search Media Posts', 'text_domain' ),
    'not_found'           => __( 'No media posts found', 'text_domain' ),
    'not_found_in_trash'  => __( 'No media posts found in Trash', 'text_domain' ),
);

$rewrite = array(
    'slug'                => 'mediapost',
    'with_front'          => true,
    'pages'               => true,
    'feeds'               => true,
);

$args = array(
    'label'               => __( 'mediapost', 'text_domain' ),
    'description'         => __( 'Post Type for Media', 'text_domain' ),
    'labels'              => $labels,
    'supports'            => array( 'title', 'editor', 'custom-fields', ),
    'taxonomies'          => array( 'year', 'type' ),
    'hierarchical'        => false,
    'public'              => true,
    'show_ui'             => true,
    'show_in_menu'        => true,
    'show_in_nav_menus'   => true,
    'show_in_admin_bar'   => true,
    'menu_position'       => 5,
    'can_export'          => true,
    'has_archive'         => true,
    'exclude_from_search' => false,
    'publicly_queryable'  => true,
    'query_var'           => 'mediapost',
    'rewrite'             => $rewrite,
    'capability_type'     => 'page',
);

register_post_type( 'special_media_post', $args );
}

// Register Custom Taxonomy
function media_year()  {
$labels = array(
    'name'                       => _x( 'Years', 'Taxonomy General Name', 'text_domain' ),
    'singular_name'              => _x( 'Year', 'Taxonomy Singular Name', 'text_domain' ),
    'menu_name'                  => __( 'Year', 'text_domain' ),
    'all_items'                  => __( 'All Years', 'text_domain' ),
    'parent_item'                => __( 'Parent Year', 'text_domain' ),
    'parent_item_colon'          => __( 'Parent Year:', 'text_domain' ),
    'new_item_name'              => __( 'New Year Name', 'text_domain' ),
    'add_new_item'               => __( 'Add New Year', 'text_domain' ),
    'edit_item'                  => __( 'Edit Year', 'text_domain' ),
    'update_item'                => __( 'Update Year', 'text_domain' ),
    'separate_items_with_commas' => __( 'Separate years with commas', 'text_domain' ),
    'search_items'               => __( 'Search years', 'text_domain' ),
    'add_or_remove_items'        => __( 'Add or remove years', 'text_domain' ),
    'choose_from_most_used'      => __( 'Choose from the most used yearss', 'text_domain' ),
);

$rewrite = array(
    'slug'                       => 'year',
    'with_front'                 => true,
    'hierarchical'               => true,
);

$capabilities = array(
    'manage_terms'               => 'manage_categories',
    'edit_terms'                 => 'manage_categories',
    'delete_terms'               => 'manage_categories',
    'assign_terms'               => 'edit_posts',
);

$args = array(
    'labels'                     => $labels,
    'hierarchical'               => true,
    'public'                     => true,
    'show_ui'                    => true,
    'show_admin_column'          => true,
    'show_in_nav_menus'          => true,
    'show_tagcloud'              => true,
    'query_var'                  => 'year',
    'rewrite'                    => $rewrite,
    'capabilities'               => $capabilities,
);

register_taxonomy( 'year', 'special_media_post', $args );
}

// Register Custom Taxonomy
function media_type()  {
$labels = array(
    'name'                       => _x( 'Types', 'Taxonomy General Name', 'text_domain' ),
    'singular_name'              => _x( 'Type', 'Taxonomy Singular Name', 'text_domain' ),
    'menu_name'                  => __( 'Type', 'text_domain' ),
    'all_items'                  => __( 'All Types', 'text_domain' ),
    'parent_item'                => __( 'Parent Type', 'text_domain' ),
    'parent_item_colon'          => __( 'Parent Type:', 'text_domain' ),
    'new_item_name'              => __( 'New Type Name', 'text_domain' ),
    'add_new_item'               => __( 'Add New Type', 'text_domain' ),
    'edit_item'                  => __( 'Edit Type', 'text_domain' ),
    'update_item'                => __( 'Update Type', 'text_domain' ),
    'separate_items_with_commas' => __( 'Separate types with commas', 'text_domain' ),
    'search_items'               => __( 'Search types', 'text_domain' ),
    'add_or_remove_items'        => __( 'Add or remove types', 'text_domain' ),
    'choose_from_most_used'      => __( 'Choose from the most used types', 'text_domain' ),
);

$rewrite = array(
    'slug'                       => 'type',
    'with_front'                 => true,
    'hierarchical'               => true,
);

$capabilities = array(
    'manage_terms'               => 'manage_categories',
    'edit_terms'                 => 'manage_categories',
    'delete_terms'               => 'manage_categories',
    'assign_terms'               => 'edit_posts',
);

$args = array(
    'labels'                     => $labels,
    'hierarchical'               => true,
    'public'                     => true,
    'show_ui'                    => true,
    'show_admin_column'          => true,
    'show_in_nav_menus'          => true,
    'show_tagcloud'              => true,
    'query_var'                  => 'media_type',
    'rewrite'                    => $rewrite,
    'capabilities'               => $capabilities,
);

register_taxonomy( 'type', 'special_media_post', $args );
}

// Hook into the 'init' action
add_action( 'init', 'special_media_post', 0 );

// Hook into the 'init' action
add_action( 'init', 'media_year', 0 );

// Hook into the 'init' action
add_action( 'init', 'media_type', 0 );

If there is something else you need to see, I can put it up, but it doesn't even see it if I put an echo 'Hello World' in there. So it is simply not seeing either the single-special_media_post.php or the archive-special_media_post.php

0

4 Answers 4

75

Visit the permalinks page (which will flush it) and check again. WordPress probably just needs to be nudged to recognize your addition to the hierarchy.

3
  • 5
    wow! works like charm, wasted 1 hour :( Sep 22, 2016 at 10:05
  • 1
    also wasted an hour, thank you! Apr 8, 2020 at 15:38
  • 1
    yep, one hour wasted. Welp. On to the next thing.
    – bwoogie
    Apr 28, 2020 at 17:33
5

Change the code

From :

 'has_archive'         => true,

To :

 'has_archive'         => false,

And then go to permalink page, change to default and go back to your "pretty permalink"

%postname%/

Now it should work.

The reason why its not going to the single-{custom_post_type}.php page is because of the has_archive. When has_archive is set on true it will look for archive-{custom_post_type}.php instead of the single page.

Hope this worked.

6
  • 1
    I'm not sure that's true. A single post view is a single post view, whether the post-type supports archive index views or not. Apr 23, 2013 at 13:07
  • This solution i posted is how i solved my problem on my self developed theme. When my single post type page would now show me what i wanted to see. Apr 23, 2013 at 13:12
  • 3
    It is possible that the fix is from resetting the permalink structure, which in turn flushed the rewrite rules. Apr 23, 2013 at 13:18
  • 2
    setting 'has_archive' has no effect on wheter or not the single-{post_type}.php template will be used for a single request.
    – Jules
    Jan 18, 2018 at 14:07
  • Works like a charm. Thank you dude! Aug 30, 2019 at 20:18
1

I copied your code, flushed the rewrite rules via the Admin and now the Theme uses the right templates when I visit a Media Post.

You need to flush the rewrite rules once using the after_switch_theme hook. This will make sure that the rewrite rules get flushed automatically after the user has activated the Theme.

You can use this code (straight from the Codex):

add_action( 'init', 'theme_prefix_cpt_init' );
function theme_prefix_cpt_init() {
    register_post_type( ... );
}

function theme_prefix_rewrite_flush() {
    flush_rewrite_rules();
}
add_action( 'after_switch_theme', 'theme_prefix_rewrite_flush' );

Please consult the WordPress Codex for more information: http://codex.wordpress.org/Function_Reference/register_post_type

EDIT: In such a case, the Inspect Rewrite Rules Plugin comes in very handy, because it allows you to see the rules connected to your custom post type: http://wordpress.org/extend/plugins/rewrite-rules-inspector/

On a sidenote please also note that the recommended place to put Custom Post Types is a Plugin, not a Theme.

1

It's a good practice also uses the register_activation_hook() and register_deactivation_hook() when creating new content types.

It seems that fresh new content types always will fail to be rewritten. To avoid this, put in the register_activation_hook() callback the flush_rewrite_rules() and your registration new content function. I don't know why, but doing this seems to avoid this problem. Look:

register_activation_hook( __FILE__, 'your_active_hook' );

function your_active_hook() {
    special_media_post();
    flush_rewrite_rules();
}

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