Create new post type in wordpress

To create a new post type in WordPress, you can use the register_post_type function in your theme's functions.php file or in a plugin. Here's an example of how to do it:

Example 1: Creating a new post type in functions.php

Add the following code to your theme's functions.php file:

function create_new_post_type() {
    register_post_type('book',
        array(
            'labels' => array(
                'name_admin_bar' => 'Book',
                'name' => __( 'Books' ),
                'singular_name' => __( 'Book' ),
                'menu_name' => __( 'Books' ),
                'all_items' => __( 'All Books' ),
                'add_new' => __( 'Add New Book' ),
                'add_new_item' => __( 'Add New Book' ),
                'edit_item' => __( 'Edit Book' ),
                'new_item' => __( 'New Book' ),
                'view_item' => __( 'View Book' ),
                'search_items' => __( 'Search Books' ),
                'not_found' => __( 'No books found' ),
                'not_found_in_trash' => __( 'No books found in trash' ),
            ),
            'public' => true,
            'has_archive' => true,
            'rewrite' => array( 'slug' => 'books' ),
            'supports' => array( 'title', 'editor', 'thumbnail' ),
        )
    );
}
add_action( 'init', 'create_new_post_type' );

In this example, we're creating a new post type called "book" with the following settings:

Example 2: Creating a new post type in a plugin

If you want to create a new post type in a plugin, you can use the same code as above, but you'll need to add it to a file in your plugin's directory. For example, you could add it to a file called post-types.php in your plugin's directory.

Here's an example of how you might do it:

<?php
/*
Plugin Name: My Plugin
Description: A plugin that creates a new post type
*/

function create_new_post_type() {
    register_post_type('book',
        array(
            'labels' => array(
                'name_admin_bar' => 'Book',
                'name' => __( 'Books' ),
                'singular_name' => __( 'Book' ),
                'menu_name' => __( 'Books' ),
                'all_items' => __( 'All Books' ),
                'add_new' => __( 'Add New Book' ),
                'add_new_item' => __( 'Add New Book' ),
                'edit_item' => __( 'Edit Book' ),
                'new_item' => __( 'New Book' ),
                'view_item' => __( 'View Book' ),
                'search_items' => __( 'Search Books' ),
                'not_found' => __( 'No books found' ),
                'not_found_in_trash' => __( 'No books found in trash' ),
            ),
            'public' => true,
            'has_archive' => true,
            'rewrite' => array( 'slug' => 'books' ),
            'supports' => array( 'title', 'editor', 'thumbnail' ),
        )
    );
}
add_action( 'init', 'create_new_post_type' );

In this example, we're creating a new plugin called "My Plugin" that creates a new post type called "book". The code is the same as in the first example, but we're adding it to a file in our plugin's directory instead of the theme's functions.php file.

Note: When creating a new post type, you'll need to make sure that the post type is registered before you can use it. You can do this by calling the register_post_type function in your plugin or theme's init hook.