Add new custom post type wordpress
Here are the steps to add a new custom post type in WordPress:
Step 1: Create a new file
Create a new file in the wp-content/plugins
directory of your WordPress installation. For example, you can name it custom-post-type-plugin.php
.
Step 2: Define the custom post type
In the new file, add the following code to define the custom post type:
<?php
/*
Plugin Name: Custom Post Type Plugin
Description: A plugin to create a custom post type
*/
function custom_post_type() {
$labels = array(
'name_admin_bar' => 'Custom Post Type',
'name' => __( 'Custom Post Type' ),
'singular_name' => __( 'Custom Post' ),
'menu_name' => __( 'Custom Post Type' ),
'all_items' => __( 'All Custom Posts' ),
'add_new' => __( 'Add New Custom Post' ),
'add_new_item' => __( 'Add New Custom Post' ),
'edit_item' => __( 'Edit Custom Post' ),
'new_item' => __( 'New Custom Post' ),
'view_item' => __( 'View Custom Post' ),
'search_items' => __( 'Search Custom Posts' ),
'not_found' => __( 'No custom posts found' ),
'not_found_in_trash' => __( 'No custom posts found in trash' ),
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'custom-post-type' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => 5,
'supports' => array( 'title', 'editor', 'thumbnail' ),
);
register_post_type( 'custom_post_type', $args );
}
add_action( 'init', 'custom_post_type' );
This code defines a custom post type called "Custom Post Type" with the following settings:
- Labels: The labels for the custom post type, such as the name, singular name, and menu name.
- Public: Whether the custom post type is publicly accessible.
- Publicly queryable: Whether the custom post type can be queried publicly.
- Show UI: Whether the custom post type should be displayed in the WordPress admin interface.
- Show in menu: Whether the custom post type should be displayed in the WordPress admin menu.
- Query var: Whether the custom post type should have a query variable.
- Rewrite: The slug for the custom post type.
- Capability type: The capability type for the custom post type (in this case, "post").
- Has archive: Whether the custom post type should have an archive page.
- Hierarchical: Whether the custom post type is hierarchical (i.e., whether it can have parent-child relationships).
- Menu position: The position of the custom post type in the WordPress admin menu.
- Supports: The features that the custom post type supports, such as the title, editor, and thumbnail.
Step 3: Activate the plugin
Go to the WordPress admin dashboard and click on the "Plugins" menu. Find the "Custom Post Type Plugin" plugin and click on the "Activate" button.
Step 4: Create a new custom post
Go to the WordPress admin dashboard and click on the "Posts" menu. You should see a new menu item called "Custom Post Type". Click on this menu item to create a new custom post.
That's it! You have now created a new custom post type in WordPress.