1. Home
  2. »
  3. WordPress Content Management
  4. »
  5. How to Create Custom Post Types

How to Create Custom Post Types

Creating Custom Post Types in WordPress Plugin Development

Custom post types are a powerful feature in WordPress that allows developers to create their own content types beyond the standard posts and pages. This can be useful for organizing different types of content on a website, such as portfolios, testimonials, products, etc.

To create a custom post type in WordPress plugin development, you can use the register_post_type() function. Here’s an example of how you can create a custom post type called “Books” in your plugin:

// Register Custom Post Type
function flashify_create_post_type() {
    $labels = array(
        'name'                  => _x( 'Books', 'Post Type General Name', 'text_domain' ),
        'singular_name'         => _x( 'Book', 'Post Type Singular Name', 'text_domain' ),
        'menu_name'             => __( 'Books', 'text_domain' ),
        'name_admin_bar'        => __( 'Book', 'text_domain' ),
        'archives'              => __( 'Book Archives', 'text_domain' ),
        'attributes'            => __( 'Book Attributes', 'text_domain' ),
        'parent_item_colon'     => __( 'Parent Book:', 'text_domain' ),
        'all_items'             => __( 'All Books', 'text_domain' ),
        'add_new_item'          => __( 'Add New Book', 'text_domain' ),
        'add_new'               => __( 'Add New', 'text_domain' ),
        'new_item'              => __( 'New Book', 'text_domain' ),
        'edit_item'             => __( 'Edit Book', 'text_domain' ),
        'update_item'           => __( 'Update Book', 'text_domain' ),
        'view_item'             => __( 'View Book', 'text_domain' ),
        'view_items'            => __( 'View Books', 'text_domain' ),
        'search_items'          => __( 'Search Book', 'text_domain' ),
        'not_found'             => __( 'Not found', 'text_domain' ),
        'not_found_in_trash'    => __( 'Not found in Trash', 'text_domain' ),
        'featured_image'        => __( 'Featured Image', 'text_domain' ),
        'set_featured_image'    => __( 'Set featured image', 'text_domain' ),
        'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
        'use_featured_image'    => __( 'Use as featured image', 'text_domain' ),
        'insert_into_item'      => __( 'Insert into book', 'text_domain' ),
        'uploaded_to_this_item' => __( 'Uploaded to this book', 'text_domain' ),
        'items_list'            => __( 'Books list', 'text_domain' ),
        'items_list_navigation' => __( 'Books list navigation', 'text_domain' ),
        'filter_items_list'     => __( 'Filter books list', 'text_domain' ),
    );
    $args = array(
        'label'                 => __( 'Book', 'text_domain' ),
        'description'           => __( 'Post Type Description', 'text_domain' ),
        'labels'                => $labels,
        'supports'              => array( 'title', 'editor', 'thumbnail', 'custom-fields' ),
        'taxonomies'            => array( 'category', 'post_tag' ),
        'hierarchical'          => false,
        'public'                => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'menu_position'         => 5,
        'menu_icon'             => 'dashicons-book-alt',
        'show_in_admin_bar'     => true,
        'show_in_nav_menus'     => true,
        'can_export'            => true,
        'has_archive'           => true,
        'exclude_from_search'   => false,
        'publicly_queryable'    => true,
        'capability_type'       => 'page',
    );
    register_post_type( 'book', $args );
}
add_action( 'init', 'flashify_create_post_type', 0 );

This code snippet registers a custom post type called “Book” with various labels, supports, and settings. You can customize the labels, supports, and other parameters according to your requirements.

After adding this code to your plugin file, you can now see a new “Books” menu item in the WordPress admin dashboard. You can start adding new books and managing them just like regular posts or pages.

Remember to always prefix your functions with a unique prefix like “flashify_” to avoid conflicts with other plugins or themes. This ensures that your custom post types are created and managed properly within your plugin.

Creating custom post types in WordPress plugin development opens up a world of possibilities for organizing and presenting content on your website. Experiment with different post types and see how they can enhance the functionality and user experience of your WordPress site!

For more information on creating custom post types in WordPress, you can refer to the WordPress Developer Handbook.

Shashika De Silva

Shashika De Silva

Hey there! I’m a seasoned PHP developer with over 10 years of experience crafting awesome WordPress plugins and themes. I specialize in creating scalable and robust solutions for WordPress and WooCommerce, ensuring everything runs smoothly. Whether it’s cross-platform software development, web development, or diving into Sheets/Excel with Appscript, Macros, and VBA, I’ve got you covered. I’m all about delivering top-notch results that go beyond expectations. Let’s team up and turn your ideas into reality, making your project shine! Looking forward to working together and achieving something remarkable!

Select By Category

Flashify.Lab

Join our team
to create the best digital solutions.

Enhance your WordPress site’s functionality with custom plugins tailored to your unique needs. Our expert developers specialize in creating robust plugins that seamlessly integrate with WooCommerce, ensuring a streamlined user experience and enhanced site performance. Transform your ideas into reality with our bespoke plugin development services today

Scroll to Top