How to create collections that are edited in admin?

I believe what you’re referring to is called “Post Types”. By default, WordPress has a complete post types you’re already familiar with – post ( news / blog ), page ( Pages ), attachment ( Media ), revision ( Page / Post Revisions ). You can read up on the full list of built in post types on The Codex – Post Types.

If you want to register a new Custom Post Type ( CPT ) you can do pretty easily with some PHP knowledge. Let’s go through and create our own post type called “Products”.

Step One – Connect to FTP – you do not want to do this using Appearance -> Editor.

Step Two – Locate your theme, if you’re using a child theme open that up and locate the functions.php file. Open the file in Notepad++ or your editor of choice.

Step Three – Register Your Post Type

Toward the top of your functions.php file we’re going to hook into WordPress init and use a built-in WordPress function cleverly called: register_post_type. I highly suggest you read these codex links as I will not explain each point but instead give a brief description.

/** Create Post Type **/
function cpt_init() {

    // Products Custom Post Type
    register_post_type( 'cpt_products', array(                  // Internal name `cpt_products` - this is what we test against in queries
        'labels'            =>  array(                          // An array of Administrative labels
            'name'          =>      __( 'Products' ),
            'singular_name' =>      __( 'Product' ),
            'all_items'     =>      __( 'View Products' ),
            'add_new'       =>      __( 'New Product' ),
            'add_new_item'  =>      __( 'New Product' ),
            'edit_item'     =>      __( 'Edit Product' ),
            'view_item'     =>      __( 'View Product' ),
            'search_items'  =>      __( 'Search Products' ),
            'no_found'      =>      __( 'No Products Found' ),
            'not_found_in_trash' => __( 'No Products in Trash' )
        ),
        'public'            =>  true,                           // Whether it will be publically available or only in the admin panel
        'publicly_queryable'=>  true,                           // "                                                                "
        'show_ui'           =>  true,                           // Whether we want this post type to show up in the admin panel
        'show_in_nav_menus' =>  false,                          // If we want Products to show up in the `Appearance -> Menu`
        'capability_type'   =>  'page',                         // Mostly used for user capability
        'hierarchical'      =>  false,                          // If you will allow products to have child products / assigned a parent
        'rewrite'           =>  array( 'slug' => 'product', 'with_front' => false ),        // This is going to be what the single post will be located at `/product/product-name/`
        'menu_icon'         =>  'dashicons-cart',               // https://developer.wordpress.org/resource/dashicons/#welcome-widgets-menus
        'menu_position'     =>  5,                              // Where you want it to show up in the admin panel
        'supports'          =>  array( 'title', 'editor', 'page-attributes', 'revisions' )  // What is actually shown when a new product is added - admin panel
    ) );
}
add_action( 'init', 'cpt_init' );

Step Four – Upload functions.php – an important note here is to also flush permalinks which is as easy as going to Settings -> Permalinks and click “Save” button at the bottom of the page.


Similar to creating a post type, if you want to create Categories ( similar to Post Categories ) you can do so using the register_taxonomy function. Follow the steps as above until Step 3.

Step 3 – Register Taxonomy

The first two parameters are important to link the taxonomy to the post type. The next important part is the hierarchical parameter – this will be the difference between post_tags ( false ) and categories ( true ). We will make this taxonomy hierarchical which is similar to post categories.

/** Add Custom Taxonomy **/
function tax_init() {
    // Product Categories
    register_taxonomy( 
        'tax_products',         // Taxonomy slug
        'cpt_products',         // What post type we're assigning the taxonomy to.
        array(                  // Array of Arguments
            'labels'            =>  array(                                  // User Friendly Labels
                'name'              => __( 'Product Categories' ),
                'singular_name'     => __( 'Product Category' ),
                'search_items'      => __( 'Search Product Categories' ),
                'all_items'         => __( 'All Product Categories' ),
                'parent_item'       => __( 'Parent Product Category' ),
                'parent_item_colon' => __( 'Parent Product Category:' ),
                'edit_item'         => __( 'Edit Product Category' ), 
                'update_item'       => __( 'Update Product Category' ),
                'add_new_item'      => __( 'Add New Product Category' ),
                'new_item_name'     => __( 'New Product Category' ),
                'menu_name'         => __( 'Product Categories' )
            ),
            'public'            =>  true,               // Whether it will be public or only used in the admin panel
            'hierarchical'      =>  true,               // The differnce between *Tags* and *Categories*
            'show_in_nav_menus' =>  true,               // To show up in `Appearance -> Menus` or not.
            'show_admin_column' =>  true,               // If we want an column next to the title when listing our products
            'rewrite'           =>  array( 'slug' => 'products/category', 'with_front' => false, 'hierarchical' => true )       // Similar to post type, where to find the categories `products/category/category-1/`
        ) 
    );
}
add_action( 'init', 'tax_init');

Leave a Comment