
Custom Post Type WordPress with PHP code
Here’s a simple example of how to create a custom post type in WordPress using PHP code:
function create_custom_post_type() { $labels = array( 'name' => 'Books', 'singular_name' => 'Book', 'add_new' => 'Add New', 'add_new_item' => 'Add New Book', 'edit_item' => 'Edit Book', 'new_item' => 'New Book', 'all_items' => 'All Books', 'view_item' => 'View Book', 'search_items' => 'Search Books', 'not_found' => 'No books found', 'not_found_in_trash' => 'No books found in Trash', 'parent_item_colon' => '', 'menu_name' => 'Books' ); $args = array( 'labels' => $labels, 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_menu' => true, 'query_var' => true, 'rewrite' => array( 'slug' => 'book' ), 'capability_type' => 'post', 'has_archive' => true, 'hierarchical' => false, 'menu_position' => null, 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ) ); register_post_type( 'book', $args ); } add_action( 'init', 'create_custom_post_type' );
In this example, the create_custom_post_type function creates a custom post type named “Book”. The $labels array defines the labels for the custom post type, such as the name, singular name, and various labels for the admin area. The $args array defines the various arguments for the custom post type, such as the public visibility, capability type, and support for various features like title, editor, author, etc.
The register_post_type function registers the custom post type with WordPress, passing in the name of the custom post type and the arguments array. The add_action function hooks the custom post type creation into the init action, so it runs when WordPress initializes.