How To Register A Custom Post Type In WordPress

Looking to register a custom post type in WordPress? All you may need is to add a function and action to your functions.php file.

Creating a custom post type

To create a custom post type you need to add a function and action to your WordPress theme’s functions.php file.

  • Navigate to Appearance > Editor > Theme Functions from your WordPress Dashboard;
  • Copy and paste in the function and action below and adjust accordingly;
  • Click Update File to save the changes.
function custom_post_type_function() {
    $labels = array(
        'name' => 'Post Types',
        'singular_name' => 'Post Type',
        'menu_name' => 'Post Types',
        'name_admin_bar' => 'Post Type',
        'add_new' => 'Add Post Type',
        'add_new_item' => 'Add New Post Type',
        'new_item' => 'New Post Type',
        'edit_item' => 'Edit Post Type',
        'view_item' => 'View Post Type',
        'all_items' => 'All Post Types',
        'search_items' => 'Search Post Types',
        'parent_item_colon' => 'Parent Post Type',
        'not_found' => 'No Post Types Found.',
        'not_found_in_trash' => 'No Post Types Found In Trash.'
    );
    $args = array(
        'labels' => $labels,
        'description' => 'Custom Post Type Description.',
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'custom-post-type-slug' ),
        'capability_type' => 'post',
        'menu_icon' => 'dashicons-media-interactive',
        'has_archive' => true,
        'hierarchical' => true,
        'menu_position' => null,
        'exclude_from_search' => false,
        'supports' => array( 'title', 'editor', 'thumbnail' )
    );
    register_post_type( 'custom_post_type_name', $args );
}

add_action( 'init', 'custom_post_type_function' );