Creating a custom post type and How to show posts in Front-end in WordPress
What is a Custom Post Type in WordPress?
A Custom Post Type (CPT) in WordPress is a content type that you can create to suit your specific needs, beyond the default post types provided by WordPress. By default, WordPress comes with a few post types such as:
- Posts (blog entries)
- Pages (static content)
- Attachments (media files)
- Revisions (previous versions of posts)
- Navigation Menus (menu items)
However, there are times when these default post types aren’t sufficient to manage all the different kinds of content you might want to display on your website. This is where Custom Post Types come in.
Why Use Custom Post Types?
- Organized Content:
- Better Organization: Custom Post Types allow you to separate different types of content logically. For example, if you run a movie review site, you might want to create a custom post type for “Movies” and another for “Reviews.”
- Customization and Flexibility:
- Tailored Content Management: Custom Post Types let you create tailored interfaces and workflows for different content types, making content management more intuitive and efficient for you and your team.
- Custom Fields and Meta Boxes: You can add custom fields and meta boxes to your custom post types, allowing for more specific and detailed content input.
- Enhanced User Experience:
- Improved Navigation and Search: Separating content into custom post types can improve site navigation and search functionality, making it easier for users to find the specific content they’re interested in.
- Unique Templates and Styling: You can assign different templates and styles to different post types, enhancing the visual presentation and user experience.
- SEO Benefits:
- Focused Content: By categorizing content more specifically, you can optimize different post types for different sets of keywords, potentially improving your site’s overall SEO performance.
- Scalability:
- Future Growth: As your site grows, custom post types can help you manage and scale your content more effectively, without cluttering your main posts or pages.
How to Create Custom Post Types in WordPress
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() ) { $books_query->the_post(); ?> <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> <div class="book-content"> <?php the_content(); ?> </div> </div> <?php } // Restore original post data wp_reset_postdata(); } else { // If no posts found echo 'No books found.'; }
Explanation:
- 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() )
andthe_post()
to iterate through each post. - Inside the loop, we use template tags like
the_title()
andthe_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.