< BACK TO BLOG

How to create Custom Post Types in WordPress

Published January 30, 2022

In this post I will show you how to create a Custom Post Types in WordPress.

This structure is good to be used when you have end users that input the content through the admin panel. It is very friendly to end users while keeps your code clean by storing the information inside variables.

Inside your functions.php, first you have to add the action, and right after it comes the function to create the Custom Post Type passing the desired params.

In this case I am creating a Project Custom Post Type, but you can use rename it to any title you would like.


    add_action( 'init', 'project_items' );

    function project_items() {

        register_post_type( 'Projects', array(
            'labels' => array(
                'name' => 'Projects',
                'singular_name' => 'Project',
                'add_new_item' => 'Add new project',
            ),
            'description' => 'Add a new Project',
        'public' => true,  // it's not public, it shouldn't have it's own permalink, and so on
        'publicly_queryable' => true,  // you should be able to query it
        'show_ui' => true,  // you should be able to edit it in wp-admin
        'exclude_from_search' => false,  // you should exclude it from search results
        'show_in_nav_menus' => true,  // you shouldn't be able to add it to menus
        'has_archive' => false,  // it shouldn't have archive page
        'rewrite' => array('slug' => 'projects','with_front' => true),  // it should have rewrite rules
        // This is where we add taxonomies to our CPT
        'menu_position' => 5,
        'menu_icon' => 'dashicons-clipboard',
        'supports' => array( 'title'),        
        'taxonomies' => array('post_tag','category', 'thumbnail', 'excerpt', 'comments')
    ));
    }

After that you can access your new Custom Post Type through the admin panel and start creating posts for it.


Subscribe to my Newsletter

Get the latest posts delivered right to your inbox

Avatar of Author

Taiguara Andrade

Fullstack Developer and Writer at taiguaras.xyz


© 2022 taiguaras