keepcalculator

06 Numbers and Powers

Create Custom Post Types in WordPress (Guide)

Card No. 06·30123 · Updated · Standard formula, shown below

Yes, you can create custom post types in WordPress to publish content types beyond posts and pages, such as portfolios, products, or testimonials. This guide explains the two main methods—using a plugin or adding code to your theme—and covers registration, taxonomies, templates, and common pitfalls.

What Are Custom Post Types?

WordPress stores all content as “posts� in the database, but it distinguishes them by their post type. By default, WordPress has several built-in types: post (blog entries), page (static pages), attachment (media), revision, and navigation_menu. A custom post type (CPT) is any additional type you register, like “Book�, “Event�, or “Team Member�.

When you register a CPT, WordPress creates a new section in the admin menu, a new database table entry (though technically it uses the same wp_posts table with a different post_type value), and allows you to define custom fields, taxonomies, and templates. This lets you separate different kinds of content so they can be managed and displayed distinctly.

Why Use a Custom Post Type?

Custom post types help organize content that doesn’t fit the standard “post� or “page� structure. For example:

  • Portfolio – showcase projects with custom fields like client, year, and link.
  • Products – list items with price, SKU, and stock status (often combined with WooCommerce).
  • Events – store date, location, and speaker details.
  • Testimonials – display quotes with author names and ratings.

Using a CPT keeps these entries separate from your blog posts, making it easier to query, style, and maintain them. It also improves the admin experience for clients who only see the relevant menu items.

How to Register a Custom Post Type

There are two primary ways: a plugin (like Custom Post Type UI) or code in your theme’s functions.php file (or a site-specific plugin). The code method gives you full control and doesn’t depend on a third-party plugin.

Here is a minimal example that registers a “Book� post type:

function create_book_post_type() {
    register_post_type( 'book',
        array(
            'labels' => array(
                'name' => __( 'Books' ),
                'singular_name' => __( 'Book' )
            ),
            'public' => true,
            'has_archive' => true,
            'supports' => array( 'title', 'editor', 'thumbnail' ),
            'menu_icon' => 'dashicons-book-alt',
        )
    );
}
add_action( 'init', 'create_book_post_type' );

Place this in your theme’s functions.php or a custom plugin. After saving, refresh the admin dashboard; you’ll see a “Books� menu item. The key arguments are:

  • public – controls visibility in the admin and on the front end.
  • has_archive – enables a post type archive page (e.g., example.com/books/).
  • supports – defines which editing features appear (title, editor, thumbnail, etc.).
  • menu_icon – sets the icon in the admin menu (use a Dashicons class).

Choosing Between Plugin and Code

For most users, a plugin like Custom Post Type UI is easier because it provides a graphical interface to register CPTs and taxonomies without touching code. It also lets you export the configuration as PHP code if you later want to move to a custom implementation.

However, coding your own registration is better if you want to avoid plugin dependencies, have complete control, or are building a custom theme. The trade-off is that you must maintain the code and ensure it remains compatible with WordPress updates.

If you use a child theme, add the code to the child theme’s functions.php so it isn’t lost when the parent theme updates. Alternatively, create a small site-specific plugin to keep functionality independent of the theme.

Adding Taxonomies and Custom Fields

Taxonomies are ways to group CPT items. For “Books�, you might add a “Genre� taxonomy (like categories) and an “Author� taxonomy (like tags). You can register them with register_taxonomy():

function create_book_taxonomies() {
    register_taxonomy( 'genre', 'book', array( 'label' => __( 'Genre' ), 'hierarchical' => true ) );
    register_taxonomy( 'author', 'book', array( 'label' => __( 'Author' ), 'hierarchical' => false ) );
}
add_action( 'init', 'create_book_taxonomies' );

Custom fields (also called post meta) store additional data like ISBN or publication year. You can add them via the built-in “Custom Fields� panel (enable it in Screen Options), or use a plugin like Advanced Custom Fields for a friendlier interface. In your template files, you can retrieve meta values with get_post_meta().

Displaying Custom Post Types on the Front End

To display your CPT, you need template files. WordPress uses a template hierarchy: for a single “book� item, it looks for single-book.php; for the archive, archive-book.php. If those don’t exist, it falls back to single.php or archive.php, then to index.php. For full control, create those files in your theme.

In single-book.php, you can loop through the post and display custom fields. For example:

<?php while ( have_posts() ) : the_post(); ?>
    <h1><?php the_title(); ?></h1>
    <?php the_post_thumbnail(); ?>
    <?php the_content(); ?>
    <p>ISBN: <?php echo get_post_meta( get_the_ID(), 'isbn', true ); ?></p>
<?php endwhile; ?>

If you don’t create a custom template, WordPress will use the generic single/archive templates, which may not show your custom fields. Always test the front end after registering a CPT.

Common Problems and How to Fix Them

  • 404 errors on CPT pages – After registering a CPT, go to Settings → Permalinks and click “Save Changesâ€� to flush rewrite rules. This is the most common fix.
  • Custom fields not saving – Ensure your form fields have proper names and that you call update_post_meta() in the save hook. If using a plugin, check its documentation.
  • CPT not showing in admin menu – Verify that public is set to true and that you’ve refreshed the page. Also check for PHP errors in your functions.php.
  • Archive page shows no posts – Make sure you have at least one published item and that has_archive is true. Also check that your theme’s archive template includes the correct query.
  • CPT items not appearing in search – By default, CPTs are not included in search unless you add them via the pre_get_posts hook. Add code to modify the main query.

Frequently Asked Questions

Do I need a plugin to create custom post types?

No. You can register CPTs with code in your theme’s functions.php or a custom plugin. Plugins like Custom Post Type UI are convenient but not required.

Will a custom post type slow down my site?

No, not inherently. The database query is similar to regular posts. Performance depends on how many items you have and how you query them. Use proper caching and avoid heavy meta queries when possible.

Can I convert existing posts to a custom post type?

Yes, but it requires a database update. You can use a plugin like “Convert Post Types� or run a SQL query to change the post_type value. Always back up your database first.

How do I add custom fields to my custom post type?

You can use the built-in Custom Fields panel (enable it in Screen Options) or install a plugin like Advanced Custom Fields. The latter provides a better UI and more field types.

Why do my custom post type URLs show 404?

Most likely because the permalink structure hasn’t been updated. Go to Settings → Permalinks and click “Save Changes� to refresh rewrite rules. If that doesn’t work, check that your CPT is registered correctly and that the template files exist.

Can I use custom post types with page builders?

Yes, most page builders (like Elementor or Beaver Builder) support CPTs. You can create templates for them and assign them via the builder’s theme builder or custom post type archive settings.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →