how to create custom post type in WordPress

Understanding Custom Post Types in WordPress

Custom post types (CPTs) are a fundamental feature in WordPress that allow you to go beyond posts and pages by creating different content types according to your specific needs. This can include anything from portfolio entries and products to testimonials and movie reviews. CPTs help organize and manage content effectively, making WordPress a robust CMS.

Step-by-Step Guide to Creating Custom Post Types

Step 1: Planning Your Custom Post Type
Before diving into code or plugins, plan what kind of content you want to manage with your custom post type. Define the attributes and metadata that each entry will have. For instance, a book review post type might have custom fields for the author, genre, and rating.

Step 2: Using a Plugin to Create Custom Post Types
For those who prefer not to touch code, plugins like ‘Custom Post Type UI’ provide a user-friendly interface for creating and managing custom post types.

  • Install and activate the ‘Custom Post Type UI’ plugin.
  • Go to CPT UI > Add/Edit Post Types in your WordPress dashboard.
  • Fill in the basic details under the ‘Add New Post Type’ section, such as Post Type Slug (e.g., book_reviews), Plural Label (e.g., Book Reviews), and Singular Label (e.g., Book Review).
  • Configure additional settings such as whether the post type supports archives, has an RSS feed, and its visibility in the admin menu.
  • Click ‘Add Post Type’ to create your custom post type.

Step 3: Manually Creating Custom Post Types via functions.php
If you prefer to code, you can add a custom post type directly in your theme’s functions.php file or through a site-specific plugin.

function create_book_reviews_cpt() {
    $labels = array(
        'name' => _x('Book Reviews', 'Post Type General Name', 'textdomain'),
        'singular_name' => _x('Book Review', 'Post Type Singular Name', 'textdomain'),
        'menu_name' => __('Book Reviews', 'textdomain'),
        'all_items' => __('All Reviews', 'textdomain'),
        'view_item' => __('View Review', 'textdomain'),
        'add_new_item' => __('Add New Review', 'textdomain'),
    );

    $args = array(
        'label' => __('book_reviews', 'textdomain'),
        'description' => __('Movie reviews with ratings and authors', 'textdomain'),
        'labels' => $labels,
        'supports' => array('title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'custom-fields',),
        'hierarchical' => false,
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 5,
        'show_in_admin_bar' => true,
        'show_in_nav_menus' => true,
        'can_export' => true,
        'has_archive' => true,
        'exclude_from_search' => false,
        'publicly_queryable' => true,
        'capability_type' => 'post',
    );
    register_post_type('book_reviews', $args);
}
add_action('init', 'create_book_reviews_cpt', 0);

Step 4: Adding Custom Fields
Custom fields are used to store additional information about the posts. You can add custom fields manually in your theme files or use plugins like Advanced Custom Fields (ACF).

  • Install and activate the ACF plugin.
  • Go to Custom Fields in your dashboard and click ‘Add New’.
  • Name your field group and click on ‘Add Field’ to create each custom attribute you planned for your CPT.
  • Set the location rule to your custom post type so the fields appear when creating or editing entries of that type.

Step 5: Displaying Custom Post Types on Your Site
Once your CPT and custom fields are set, you need to display them on the front end. This involves editing theme files like single.php or creating a new template file specifically for your CPT (e.g., single-book_reviews.php).



<?php if (have_posts()) : 
   while (have_posts()) : the_post();
      the_title(''); 
      the_content();
      // Display custom fields data
      echo '

Author: ' . get_post_meta(get_the_ID(), 'author', true) . ''; echo '

Rating: ' . get_post_meta(get_the_ID(), 'rating', true) . '

'; endwhile; endif; ?>

Step 6: SEO Optimization
Remember to optimize your custom post type for search engines. This includes setting up proper SEO titles, meta descriptions, and using relevant keywords in your content and custom fields. Plugins like Yoast SEO can help extend SEO capabilities to your custom post types.

By following these steps, you’ve not only expanded the functionality of your WordPress site but also tailored it to fit your unique content strategy, enhancing both user engagement and SEO.

Comments

Leave a Reply

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