How to Create Custom Post Types in WordPress

How to Create Custom Post Types in WordPress (2023 Guide)

Are you looking to add new content types to your WordPress site beyond standard posts and pages? Custom post types allow you to create additional content sections like portfolio projects, testimonials, events, courses and more.

In this in-depth guide, we‘ll cover everything you need to know about WordPress custom post types including:

  • What custom post types are and why they are useful
  • How to manually create custom post types using code
  • How to create custom post types using plugins
  • Tips for displaying custom post types on your site
  • Best practices and things to consider

Let‘s dive in!

What Are WordPress Custom Post Types?

In WordPress, post types are used to differentiate between different types of content. By default, WordPress comes with these post types:

  • Posts – for blog posts and articles
  • Pages – for static pages like your About and Contact pages
  • Attachments – for media files uploaded to your site
  • Revisions – for storing post revisions and autosaves
  • Navigation Menus – to create custom navigation menus

While these default post types cover the needs of a basic website or blog, you may want to publish other types of content that don‘t fit neatly into a standard post or page format. This is where custom post types come in handy.

A custom post type essentially lets you create a new type of content with its own attributes and display settings. Some examples of custom post types you might want to create:

  • Products for an ecommerce store
  • Portfolio projects to showcase your work
  • Listings in a directory or classified ads site
  • Real estate properties for a realtor or housing site
  • Courses for an eLearning platform
  • Events and webinars
  • Testimonials to highlight customer reviews
  • Staff or team member profiles
  • Recipes for a food blog

The possibilities are endless! With custom post types, you can keep different types of content organized and make use of WordPress‘ built-in content management and editing features.

Many popular WordPress plugins rely on custom post types behind the scenes. For example:

  • WooCommerce creates a custom post type called "product"
  • The Events Calendar uses a custom post type for events
  • MemberPress uses custom post types for membership levels and protected content

So chances are you‘re already using custom post types on your site without even realizing it!

Now that you understand what custom post types are and why they are useful, let‘s look at how to create them. There are two methods: manually with code or by using a plugin.

How to Manually Create a Custom Post Type in WordPress

If you‘re comfortable adding code to your WordPress site, you can create a custom post type manually by defining it in your theme‘s functions.php file or by using a code snippet plugin.

Here are the steps to manually register a custom post type:

  1. Open your theme‘s functions.php file or create a new snippet using a plugin like Code Snippets or WPCode.

  2. Add the following code, replacing "book" and "Books" with your desired post type name and label:

function create_book_post_type() {
    register_post_type(‘book‘,
        array(
            ‘labels‘      => array(
                ‘name‘          => __(‘Books‘, ‘textdomain‘),
                ‘singular_name‘ => __(‘Book‘, ‘textdomain‘),
            ),
            ‘public‘      => true,
            ‘has_archive‘ => true,
            ‘rewrite‘     => array( ‘slug‘ => ‘books‘ ), 
            ‘supports‘    => array( ‘title‘, ‘editor‘, ‘thumbnail‘, ‘excerpt‘ ),
        )
    );
}
add_action(‘init‘, ‘create_book_post_type‘);
  1. Customize the arguments passed to the register_post_type() function:
  • ‘labels‘ – Defines the labels used for the post type in the WordPress admin. The ‘name‘ is the plural label and ‘singular_name‘ is used when referencing a single item.

  • ‘public‘ – Controls whether the post type is publicly accessible on the frontend of your site. Setting this to "true" will make the post type visible.

  • ‘has_archive‘ – When set to "true", WordPress will create an archive page to list all posts of this type. For example, example.com/books

  • ‘rewrite‘ – Defines the permalink structure for the post type. The ‘slug‘ parameter sets the base slug used in URLs.

  • ‘supports‘ – Specifies which WordPress features the post type supports, like a title, block editor, featured image, excerpts, comments, revisions, etc.

There are dozens of additional arguments you can use to configure your post type. Check the register_post_type() documentation for more options.

  1. If your theme is translation-ready, replace ‘textdomain‘ in the code above with your theme‘s text domain to make the post type labels translatable.

  2. Save your changes.

Your new custom post type will now appear in the WordPress admin sidebar! You can start creating posts just like normal.

How to Create a Custom Post Type using a Plugin

If you‘re not comfortable adding code directly to your site, you can create custom post types the easy way by using a plugin.

Here are a few of the most popular custom post type plugins for WordPress:

  1. Pods – Pods is a free plugin that lets you create custom post types, custom taxonomies, and custom fields. It has an intuitive admin interface for defining your post types.

  2. Toolset Types – Toolset is a powerful suite of plugins for extending WordPress. With the Toolset Types plugin, you can create custom post types and taxonomies as well as customize templates for displaying them.

  3. Custom Post Type UI – Custom Post Type UI is a free plugin dedicated to creating and managing custom post types and taxonomies in WordPress. It has an easy to use admin panel.

For this example, we‘ll use the free Custom Post Type UI plugin.

  1. Install and activate the Custom Post Type UI plugin.

  2. Go to CPT UI > Add New in the WordPress admin.

  3. Enter a name for your custom post type, both plural and singular. For example, "Products" and "Product".

  4. Choose a slug for your post type. This will be used in the URL structure and for template names, so use lowercase and no spaces.

  5. Customize the labels, if desired, and choose whether to make the post type public, exclude it from search, show the admin UI, etc.

  6. The "Supports" section lets you enable or disable different features for the post type, such as the title, editor, featured image, comments, and more. Check the boxes for the features you want to use.

  7. Click the "Add Post Type" button to register your new custom post type.

That‘s it! The new custom post type will now be available to use on your site. You can start creating new posts just like normal.

How to Display Custom Post Types on Your WordPress Site

Now that you‘ve created a custom post type, you‘ll likely want to display it somewhere on your site. Here are a few different ways you can showcase your custom post types:

  1. Custom Post Type Archive Page

When you register a custom post type and set "has_archive" to true, WordPress automatically creates an archive page to list all published posts. For a "book" post type, the archive would be located at example.com/books.

To customize the appearance of the archive page, you can create a file in your theme called archive-{post_type}.php. Replace {post_type} with the name of your custom post type, e.g. archive-book.php.

If no specific archive template is found, WordPress will fall back to the generic archive.php or index.php template.

  1. Custom Single Post Templates

When viewing an individual custom post, WordPress uses single.php or singular.php templates to render the page. You can create a custom template for your post type by adding a file called single-{post_type}.php to your theme.

For example, single-book.php would be used for individual "book" posts. You can copy single.php as a starting point and customize it to fit the design and layout you want for your custom post type.

  1. Displaying Custom Post Types on the Front Page

To display your custom post types on the homepage alongside regular posts, you‘ll need to modify the main post query using the pre_get_posts hook.

Add this code to your theme‘s functions.php file or use a code snippet plugin:

function add_custom_post_types_to_query( $query ) {
    if ( is_home() && $query->is_main_query() ) {
        $query->set( ‘post_type‘, array( ‘post‘, ‘book‘ ) );
    }
    return $query;
}
add_action( ‘pre_get_posts‘, ‘add_custom_post_types_to_query‘ );

Be sure to replace "book" with the name of your custom post type. This will instruct WordPress to include both standard posts and your custom post type in the homepage post loop.

  1. Querying Custom Post Types in Template Files

You can use the WP_Query class to fetch custom post types in your theme templates. This allows you to display custom post types virtually anywhere on your site.

For instance, to display a list of your 5 most recent "book" posts, you could use:

$args = array(
    ‘post_type‘ => ‘book‘,
    ‘posts_per_page‘ => 5
);

$book_query = new WP_Query( $args );

if ( $book_query->have_posts() ) {
    while ( $book_query->have_posts() ) {
        $book_query->the_post();
        // Display post content here
    }
    wp_reset_postdata();
}
  1. Using Widgets

WordPress doesn‘t provide a default widget for displaying custom post types. However, you can use a plugin like Custom Post Type Widgets to create sidebar widgets for your custom content.

The free plugin adds widgets for displaying recent posts, taxonomies, and more filtered by post type. This makes it easy to feature custom post type content in your widget areas.

Best Practices for Working with Custom Post Types

Here are a few tips and best practices to keep in mind when creating and using custom post types on your WordPress site:

  • Use a descriptive and clear name for your custom post type that reflects the type of content it will store. Avoid overly generic names.

  • Choose an appropriate slug for your post type URLs. Use lowercase letters, numbers, and hyphens only.

  • Be selective about which features and settings you enable for your custom post type. Don‘t turn on unnecessary options that you won‘t use.

  • Keep your custom post type content separate from posts and pages for better content organization. Avoid mixing different content types when possible.

  • Use custom taxonomies to categorize and filter content within your custom post type. You can use the built-in Categories and Tags or create your own with register_taxonomy().

  • Take advantage of the "Supports" settings to customize the editing experience for your custom post types. Remove metaboxes and features that aren‘t needed.

  • Create custom templates to control how your custom post types are displayed to visitors. Use template hierarchy to provide fallbacks.

Additional Resources

Here are some helpful resources for working with WordPress custom post types:

  • WordPress Codex: register_post_type() function reference
  • WordPress Developer Resources: Custom Post Types
  • Plugin Directory: Custom Post Types plugins
  • WPBeginner: How to Create Custom Post Types in WordPress

Conclusion

Custom post types are an essential tool for adding different types of content to your WordPress site. Whether you‘re building an ecommerce store, a portfolio site, a directory, or an online course platform, custom post types provide the flexibility to publish and manage your content the way you want.

You can create custom post types manually with code or by using plugins like Pods, Toolset Types, or Custom Post Type UI. Once registered, custom post types work much like regular WordPress posts, with the ability to customize the admin labels, editing experience, and display settings.

To display your custom post types, you can create custom archive and single post templates, include the post type in your homepage query, or fetch the posts in template files. You can also display custom post type content using sidebar widgets.

By following best practices like using clear names, keeping content organized, and creating custom templates, you can effectively use custom post types to extend WordPress to fit your needs.

Hopefully this guide has helped you understand what WordPress custom post types are, how to create them, and how to display them on your website!

Did you like this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

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