create custom post type in wordpress – How to Create Custom Post Types in WordPress?

create custom post type in wordpress Use this tool to create custom code for Post Types with register_post_type() function. How to Create a Custom Post Type in WordPress Using Code or Plugin?

create custom post type in wordpress

A custom post type is nothing more than a regular post with a different post_type value in the database.

following built-in post types:

  • Post
  • Page
  • Attachment
  • Revision
  • Nav Menu

Complete Steps to Create Custom Post Types in WordPress

Creating a Custom Post Type Manually : functions.php

add_action( 'init', 'create_custom_post_type' );

function create_custom_post_type() {

$args = array(
  'labels' => array(
   'name' => __( 'movies' ),
   'singular_name' => __( 'movies' )
  ),
  'public' => true,
  'has_archive' => false,
  'rewrite' => array('slug' => 'movies'),
 );

register_post_type( 'movies',$args);
}
// Hooking up our function to theme setup

use init for the hook in add_action(). The register_post_type()

Read Also:  how to select category and subcategory dropdown in php MySQL?

//Init Hook for the custom post type

add_action('init', 'create_custom_post_type');

function create_custom_post_type() {

$all_functionality = array(
'title', // movie title
'editor', // movie content
'designation', // movie designation
'thumbnail', // featured images
'excerpt', // movie excerpt
'custom-fields', // custom fields
'comments', // movie comments
'revisions', // movie revisions
'post-formats', // movie formats
);

$labels = array(
'name' => _x('movies', 'plural'),
'singular_name' => _x('movies', 'singular'),
'menu_name' => _x('movies', 'admin menu'),
'name_admin_bar' => _x('movies', 'admin bar'),
'add_new' => _x('Add New', 'add new'),
'add_new_item' => __('Add New movies'),
'new_item' => __('New movies'),
'edit_item' => __('Edit movies'),
'view_item' => __('View movies'),
'all_items' => __('All movies'),
'search_items' => __('Search movies'),
'not_found' => __('No movies found.'),
);

$args = array(
'supports' => $all_functionality,
'labels' => $labels,
'description' => 'Holds our movies and specific data',
'public' => true,
'taxonomies' => array( 'category', 'post_tag' ),
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'can_export' => true,
'capability_type' => 'post',
 'show_in_rest' => true,
'query_var' => true,
'rewrite' => array('slug' => 'movies'),
'has_archive' => true,
'hierarchical' => false,
'menu_position' => 6,
'menu_icon' => 'dashicons-megaphone',
);

register_post_type('movies', $args); // Register Post type
}

Create a Template for Archive List

your theme folder/template-movies.php

<?php
/*Template Name: movies*/
get_header();
query_posts(array(
   'post_type' => 'movies'
)); ?>
<?php
while (have_posts()) : the_post(); ?>
<h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
<p><?php the_excerpt(); ?></p>
<?php endwhile;
get_footer();

Create a Detail Page of Custom Post Type

single-movies.php

<?php
get_header();

while (have_posts()) : the_post();
   get_template_part('template-parts/content', get_post_format());
endwhile; // End of the loop.
get_footer();

Let’s take a look at below code parts

Read Also:  WooCommerce Get Product Images

Added your theme content.php

template-parts/content

get_template_part('template-parts/content', get_post_format());

Don’t Miss : how to create custom post type in wordpress step by-step?

I hope you get an idea about create custom post type in wordpress.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.