Search code examples
wordpresswordpress-plugin-creation

Second custom post type no showing on frontend


The post-type code for WP is working and shows up on the frontend. But when duplicate it and replace "collaborations" to "movies", it works on the admin side but returns 404 on the frontend.

I remove the "movie" codes and tried some examples found on the net and the same issue is still there. It works for 1 post type but not more than 1. Any reason why?

The "collaboration" code goes to "www.website.com/collaborations" which is correct but the second goes to "www.website.com/movies/movies/" instead. I added the "movie" codes below.

Collaboration codes

function create_collaborations()
{
    register_post_type('collaborations',
        array('labels' => array('name' => __('collaborations'),'singular_name' => __('Collaborations')),'public' => true,'has_archive' => false,'rewrite' => array('slug' => 'collaborations'))
    );
}
add_action('init','create_collaborations');

function cw_post_type_collaborations()
{
    $supports = array('title', 'editor', 'thumbnail', 'revisions');

    $labels = array(
        'name'          => _x('Collaborations', 'plural'),
        'singular_name' => _x('Collaborations', 'singular'),
        'menu_name'     => _x('Collaborations', 'admin menu'),
        'name_admin_bar'=> _x('Collaborations', 'admin bar'),
        'add_new'       => _x('Add New', 'add new'),
        'add_new_item'  => __('Add New Collaborations'),
        'new_item'      => __('New Collaborations'),
        'edit_item'     => __('Edit Collaborations'),
        'view_item'     => __('View Collaborations'),
        'all_items'     => __('View Collaborations'),
        'search_items'  => __('Search Collaborations'),
        'not_found'     => __('No Collaborations found.')
    );

    $args = array(
        'supports'      => $supports,
        'labels'        => $labels,
        'public'        => true,
        'query_var'     => true,
        'rewrite'       => array('slug' => 'collaborations'),
        'has_archive'   => true,
        'hierarchical'  => false
    );

    register_post_type('collaborations', $args);
}
add_action('init', 'cw_post_type_collaborations');

Movie Codes

function create_movies()
{
    register_post_type('movies',
        array('labels' => array('name' => __('movies'),'singular_name' => __('Movies')),'public' => true,'has_archive' => false,'rewrite' => array('slug' => 'movies'))
    );
}
add_action('init','create_movies');

function cw_post_type_movies()
{
    $supports = array('title', 'editor', 'thumbnail', 'revisions');

    $labels = array(
        'name'          => _x('Movies', 'plural'),
        'singular_name' => _x('Movies', 'singular'),
        'menu_name'     => _x('Movies', 'admin menu'),
        'name_admin_bar'=> _x('Movies', 'admin bar'),
        'add_new'       => _x('Add New', 'add new'),
        'add_new_item'  => __('Add New Movies'),
        'new_item'      => __('New Movies'),
        'edit_item'     => __('Edit Movies'),
        'view_item'     => __('View Movies'),
        'all_items'     => __('View Movies'),
        'search_items'  => __('Search Movies'),
        'not_found'     => __('No Movies found.')
    );

    $args = array(
        'supports'      => $supports,
        'labels'        => $labels,
        'public'        => true,
        'query_var'     => true,
        'rewrite'       => array('slug' => 'movies'),
        'has_archive'   => true,
        'hierarchical'  => false
    );

    register_post_type('movies', $args);
}
add_action('init', 'cw_post_type_movies');

Solution

  • Why do you make the same post-type two times?

    Maybe I´m wrong but I think you only need the second functions.

    1.) function cw_post_type_movies() 
    2.) function cw_post_type_collaborations()
    

    For movies just use:

    function cw_post_type_movies(){
    $supports = array('title', 'editor', 'thumbnail', 'revisions');
    
    $labels = array(
        'name'          => _x('Movies', 'plural'),
        'singular_name' => _x('Movies', 'singular'),
        'menu_name'     => _x('Movies', 'admin menu'),
        'name_admin_bar'=> _x('Movies', 'admin bar'),
        'add_new'       => _x('Add New', 'add new'),
        'add_new_item'  => __('Add New Movies'),
        'new_item'      => __('New Movies'),
        'edit_item'     => __('Edit Movies'),
        'view_item'     => __('View Movies'),
        'all_items'     => __('View Movies'),
        'search_items'  => __('Search Movies'),
        'not_found'     => __('No Movies found.')
    );
    
    $args = array(
        'supports'      => $supports,
        'labels'        => $labels,
        'public'        => true,
        'query_var'     => true,
        'rewrite'       => array('slug' => 'movies'),
        'has_archive'   => true,
        'hierarchical'  => false
    );
    
    register_post_type('movies', $args);
    }
    add_action('init', 'cw_post_type_movies');
    

    For Collaborations use:

    function cw_post_type_collaborations(){
    $supports = array('title', 'editor', 'thumbnail', 'revisions');
    
    $labels = array(
        'name'          => _x('Collaborations', 'plural'),
        'singular_name' => _x('Collaborations', 'singular'),
        'menu_name'     => _x('Collaborations', 'admin menu'),
        'name_admin_bar'=> _x('Collaborations', 'admin bar'),
        'add_new'       => _x('Add New', 'add new'),
        'add_new_item'  => __('Add New Collaborations'),
        'new_item'      => __('New Collaborations'),
        'edit_item'     => __('Edit Collaborations'),
        'view_item'     => __('View Collaborations'),
        'all_items'     => __('View Collaborations'),
        'search_items'  => __('Search Collaborations'),
        'not_found'     => __('No Collaborations found.')
    );
    
    $args = array(
        'supports'      => $supports,
        'labels'        => $labels,
        'public'        => true,
        'query_var'     => true,
        'rewrite'       => array('slug' => 'collaborations'),
        'has_archive'   => true,
        'hierarchical'  => false
    );
    
    register_post_type('collaborations', $args);
    }
    add_action('init', 'cw_post_type_collaborations'); 
    

    After creating both custom-post-types go to:

    your-domain/wp-admin

    Settings > Permalinks > Save Changes

    => Permalinks are flushed