How to Create Custom Post Type in WordPress

How to Create Custom Post Type in WordPress
508 Views
0
(0)

Custom post types are same like posts and pages. By default WordPress comes with a few different post types which are all stored in the database under the wp_posts table.

Default Post Types

  • Posts
  • Pages
  • Attachments
  • Revisions
  • Navigation Menus
  • Custom CSS
  • Changesets

We can create your own custom post types and call them whatever we want. On below example we register a post type ‘movies’ with an array of arguments. These arguments are the options of our custom post type. Code goes in function.php file of your active child theme (or active theme).

<?php 
add_action( 'init', 'create_movies_post_type', 0 );

function create_movies_post_type() {

	/* Set UI labels for Movies Post Type */

	$labels = array(
		'name'                => _x( 'Movies', 'Post Type Normal Name', 'cxc-codexcoach' ),
		'singular_name'       => _x( 'Movie', 'Post Type Singular Name', 'cxc-codexcoach' ),
		'menu_name'           => __( 'Movies', 'cxc-codexcoach' ),
		'parent_item_colon'   => __( 'Parent Movie', 'cxc-codexcoach' ),
		'all_items'           => __( 'All Movies', 'cxc-codexcoach' ),
		'view_item'           => __( 'View Movie', 'cxc-codexcoach' ),
		'add_new_item'        => __( 'Add New Movie', 'cxc-codexcoach' ),
		'add_new'             => __( 'Add New', 'cxc-codexcoach' ),
		'edit_item'           => __( 'Edit Movie', 'cxc-codexcoach' ),
		'update_item'         => __( 'Update Movie', 'cxc-codexcoach' ),
		'search_items'        => __( 'Search Movie', 'cxc-codexcoach' ),
		'not_found'           => __( 'Not Found', 'cxc-codexcoach' ),
		'not_found_in_trash'  => __( 'Not found in Trash', 'cxc-codexcoach' ),
	);

	/* Other arguments for Movies Post Typ- */

	$args = array(
		'label'               => __( 'movies', 'cxc-codexcoach' ),
		'description'         => __( 'Movie news and reviews', 'cxc-codexcoach' ),
		'labels'              => $labels,
		'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
		'taxonomies'          => array( 'genres' ),
		'hierarchical'        => false,
		'public'              => true,
		'show_ui'             => true,
		'show_in_menu'        => true,
		'show_in_nav_menus'   => true,
		'show_in_admin_bar'   => true,
		'menu_position'       => 5,
		'can_export'          => true,
		'has_archive'         => true,
		'exclude_from_search' => false,
		'publicly_queryable'  => true,
		'capability_type'     => 'post',
		'show_in_rest' => true,
	);

	/* Registering your Movies Post Type */
	register_post_type( 'movies', $args );

}

How useful was this blog?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this blog.

1 comment

  1. Wow, marvelous blog layout! How long have you been blogging for?
    you make blogging look easy. The overall look of your site is
    magnificent, as well as the content!

Leave a comment

Your email address will not be published. Required fields are marked *