• Sam

    (@forbiddenchunk)


    I’ve added custom posts for years and never had problems, but today is a first.

    I’ve added my first CPT and it works fine, I then wanted to add a second CPT and now the first doesn’t appear and ‘Page not found’

    This is my code;

    add_action("init", "custom_post_types");
    function custom_post_types()
    {
    
        register_post_type('products', 
            array('label' => __('Products','templatic'),
                'labels' => array(  'name' => __('Products','templatic'),
                'singular_name' => __('Product','templatic'),
                'add_new' => __('Add Product','templatic'),
                'add_new_item'=> __('Add New Products','templatic'),
                'edit' => __('Edit','templatic'),
                'edit_item'=> __('Edit Product','templatic'),
                'new_item' => __('New Product','templatic'),
                'view_item' => __('View Products','templatic'),
                'search_items' => __('Search Products','templatic'),
                'not_found' => __('No Products found','templatic'),
                'not_found_in_trash'=> __('No Products found in trash','templatic')   
               ),
                'public'            => true,
                'can_export'        => true,
                'show_ui'           => true, // UI in admin panel
                '_builtin'          => false, // It's a custom post type, not built in
                '_edit_link'        => 'post.php?post=%d',
                'capability_type'   => 'post',
                'menu_icon'         => 'dashicons-cart',
                'hierarchical'      => false,
                'rewrite'           => array("slug" => "products"), // Permalinks
                'query_var'         => "true", // This goes to the WP_Query schema
                'supports'          => array(   'title',
                                                'thumbnail',
                                                'editor', 
                                                'revisions') ,
                'show_in_nav_menus' => true ,
            )
        ); 
    
        register_post_type('accessories', 
            array('label' => __('Accessories','templatic'),
                'labels' => array(  'name' => __('Accessories','templatic'),
                'singular_name' => __('Accessory','templatic'),
                'add_new' => __('Add Accessory','templatic'),
                'add_new_item'=> __('Add New Accessories','templatic'),
                'edit' => __('Edit','templatic'),
                'edit_item'=> __('Edit Accessory','templatic'),
                'new_item' => __('New Accessory','templatic'),
                'view_item' => __('View Accessories','templatic'),
                'search_items' => __('Search Accessories','templatic'),
                'not_found' => __('No Accessories found','templatic'),
                'not_found_in_trash'=> __('No Accessories found in trash','templatic')   
               ),
                'public'            => true,
                'can_export'        => true,
                'show_ui'           => true, // UI in admin panel
                '_builtin'          => false, // It's a custom post type, not built in
                '_edit_link'        => 'post.php?post=%d',
                'capability_type'   => 'post',
                'menu_icon'         => 'dashicons-screenoptions',
                'hierarchical'      => false,
                'rewrite'           => array("slug" => "accessory"), // Permalinks
                'query_var'         => "true", // This goes to the WP_Query schema
                'supports'          => array(   'title',
                                                'thumbnail',
                                                'editor', 
                                                'revisions') ,
                'show_in_nav_menus' => true ,
            )
        );   
    
    }

    I’ve got two single files;
    single-products.php
    single-accessories.php

    When I delete the accessories CPT, products re-appears. I wonder if I’ve made a mistake somewhere but can’t see it….

Viewing 1 replies (of 1 total)
  • lianpb

    (@lianpb)

    WCBRS 2019 Contributor

    Hello Sam,

    I have just tested your code and can see both custom post types in the backend, are you referring to the front end files conflicting? This might be because in your custom post types you have not specified whether the custom post types are using archive pages.

    'has_archive' => false
    or
    'has_archive' => true

    Please let me know if my suggestion has resolved the issue by marking this post as resolved.

Viewing 1 replies (of 1 total)

The topic ‘Second custom post type breaking the first’ is closed to new replies.