Creating a custom post type in WordPress involves registering a new post type using the register_post_type() function. Below is an example of how to create a custom post type called “Books” with some basic details:

// Add this code to your theme's functions.php file

function custom_post_type_books() {
$labels = array(
'name' => _x( 'Books', 'post type general name', 'your-text-domain' ),
'singular_name' => _x( 'Book', 'post type singular name', 'your-text-domain' ),
'menu_name' => _x( 'Books', 'admin menu', 'your-text-domain' ),
'name_admin_bar' => _x( 'Book', 'add new on admin bar', 'your-text-domain' ),
'add_new' => _x( 'Add New', 'book', 'your-text-domain' ),
'add_new_item' => __( 'Add New Book', 'your-text-domain' ),
'new_item' => __( 'New Book', 'your-text-domain' ),
'edit_item' => __( 'Edit Book', 'your-text-domain' ),
'view_item' => __( 'View Book', 'your-text-domain' ),
'all_items' => __( 'All Books', 'your-text-domain' ),
'search_items' => __( 'Search Books', 'your-text-domain' ),
'parent_item_colon' => __( 'Parent Books:', 'your-text-domain' ),
'not_found' => __( 'No books found.', 'your-text-domain' ),
'not_found_in_trash' => __( 'No books found in Trash.', 'your-text-domain' )

$args = array(
'labels' => $labels,
'description' => __( 'Description.', 'your-text-domain' ),
'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','excerpt', 'trackbacks','custom-fields', 'comments','revisions', 'thumbnail', 'author','page-attributes' )

register_post_type( 'book', $args );
register_taxonomy('book_category', 'book', array('hierarchical' => true, 'label' => 'Category', 'query_var' => true,'rewrite' => array( 'slug' => 'book-category' )));// Register Taxonomy
add_action( 'init', 'custom_post_type_books' );
In the above code:
  • custom_post_type_books() is a function that defines the custom post type.
  • $labels defines various labels for the custom post type.
  • $args defines the arguments for registering the post type, including its name, labels, capabilities, and supported features.
  • register_taxonomy() is the function used to register the custom post type taxonomy or term.
  • register_post_type() is the function used to register the custom post type.

After adding this code, you will see a new “Books” menu item in the WordPress admin dashboard, where you can add, edit, and manage books, much like regular posts or pages.

How to Display the Custom posts on your website?

To display custom posts in WordPress, you typically create a custom query to retrieve the posts and then loop through them to display their content. Below is an example of how you can display custom posts of type “Books” using a custom query:

// Custom query to retrieve 'Books' custom post type
$books_query = new WP_Query( array(
'post_type' => 'book', // Custom post type slug
'posts_per_page' => -1, // Retrieve all posts
) );

// Check if there are any posts
if ( $books_query->have_posts() ) {
// Start the loop
while ( $books_query->have_posts() ) {
<div class="book">
<h2><?php the_title(); ?></h2>//Show the title of the post
<div><?php the_content(); ?> </div>//Show the content of the post
<div class="book-meta">
<!-- Display custom meta data if needed -->
<p>Author: <?php echo get_post_meta( get_the_ID(), 'author', true ); ?></p>
<p>Published Year: <?php echo get_post_meta( get_the_ID(), 'published_year', true ); ?></p>
<!-- Display other custom meta data as needed -->
<div class="book-content">
<?php the_content(); ?>
// Restore original post data
} else {
// If no posts found
echo 'No books found.';
  • We start by creating a custom query using WP_Query, specifying the ‘post_type’ parameter to retrieve posts of the custom post type “book” and setting ‘posts_per_page’ to -1 to retrieve all posts.
  • We then check if there are any posts returned by the query using have_posts().
  • If there are posts, we start the loop using while ( $books_query->have_posts() ) and the_post() to iterate through each post.
  • Inside the loop, we use template tags like the_title() and the_content() to display the title and content of each post respectively.
  • We can also retrieve and display custom post meta data using get_post_meta() function if needed.
  • Finally, we restore the global post data using wp_reset_postdata() after the loop.

You can place this code in a WordPress template file (such as single-book.php, archive-book.php, or page.php) where you want to display the custom posts. Alternatively, you can create a custom page template and assign it to a specific page from the WordPress admin dashboard.