How to create a Custom Post Type in WordPress without Plugin

Hey there! So, you’re looking to create a custom post type (CPT) in WordPress without using a plugin? You’re in the right place!

This is something I had to learn the hard way when I first started developing custom websites. Plugins are great, don’t get me wrong, but sometimes you just want to have full control and understand the inner workings of WordPress.

Trust me, once you get the hang of this, you’ll feel like a true WordPress pro!

How to create a Custom Post Type in WordPress without Plugin

Why Create a Custom Post Type?

Let me quickly explain what a custom post type is and why you might need one. By default, WordPress comes with a few post types like Posts and Pages, but if you want something more specific – say, a “Movie” post type for a movie review site or a “Portfolio” for a photographer’s website – you’ll need a custom post type.

Now, I remember the first time I needed a custom post type. I was building a website for a small business, and I wanted to display testimonials in a unique way. At first, I used a plugin to create a custom post type, but later on, I realized I could do it myself in the theme’s functions.php file. It gave me more control, and I didn’t have to rely on a plugin to manage it!

Let’s Dive In: How to Create a Custom Post Type

Don’t worry – it’s easier than it sounds! Here’s a step-by-step guide:

1. Access Your Theme’s Functions File

First thing’s first, you’ll need access to your theme’s functions.php file. You can either do this through the WordPress admin panel or via FTP.

  • In the admin panel, go to Appearance > Theme Editor, then find the functions.php file on the right-hand side.
  • Or, if you’re using FTP, navigate to /wp-content/themes/your-theme/ and open the functions.php file.

2. Add the Code to Register Your Custom Post Type

Here’s the fun part! Now, you’re going to add some code that tells WordPress how to handle your new custom post type.

Let’s say we’re creating a “Movie” post type. You’ll want to add the following code to your functions.php file:

function create_movie_post_type() {
    $args = array(
        'labels' => array(
            'name' => 'Movies',
            'singular_name' => 'Movie',
            'add_new' => 'Add New Movie',
            'add_new_item' => 'Add New Movie',
            'edit_item' => 'Edit Movie',
            'new_item' => 'New Movie',
            'view_item' => 'View Movie',
            'search_items' => 'Search Movies',
            'not_found' => 'No movies found',
            'not_found_in_trash' => 'No movies found in Trash',
            'all_items' => 'All Movies',
            'menu_name' => 'Movies',
        ),
        'public' => true,
        'has_archive' => true,
        'show_in_rest' => true, // This makes it available in Gutenberg
        'supports' => array('title', 'editor', 'thumbnail'),
        'rewrite' => array('slug' => 'movies'),
    );
    register_post_type('movie', $args);
}

add_action('init', 'create_movie_post_type');

What’s Happening Here?

  • Labels: This part defines the names you’ll see in the WordPress dashboard. It includes everything from “Add New Movie” to “View Movie.”
  • Public: This makes your custom post type visible to visitors on the front end of your website.
  • Has Archive: This allows WordPress to create an archive page for your post type (useful if you want a listing page).
  • Show in REST: This is for compatibility with the Gutenberg editor and the WordPress API.
  • Supports: Here, you can define what features your custom post type will support, like the title, editor, and thumbnail.
  • Rewrite: This defines the URL slug. So, instead of yourdomain.com/post-type/movie-title, it will be yourdomain.com/movies/movie-title.

3. Save the File

After adding this code, save your functions.php file. Your custom post type should now be registered!

4. Test It Out

Head over to your WordPress dashboard, and you should now see a new menu item labeled Movies (or whatever name you gave your custom post type). Click on it, and you’ll see options to add new movies, edit, and view them.

Create a movie post, add a title, a description, and maybe an image, and hit Publish. You now have your very own custom post type!

Additional Customizations (Optional)

While the basics are great, you can always tweak things further. For instance, let’s say you want to add custom taxonomies (like genres for your movies). You can register taxonomies just like you did for the custom post type.

Here’s a quick example of adding a genre taxonomy to our Movies custom post type:

function create_movie_taxonomy() {
    $args = array(
        'hierarchical' => true,
        'labels' => array(
            'name' => 'Genres',
            'singular_name' => 'Genre',
            'search_items' => 'Search Genres',
            'all_items' => 'All Genres',
            'parent_item' => 'Parent Genre',
            'parent_item_colon' => 'Parent Genre:',
            'edit_item' => 'Edit Genre',
            'update_item' => 'Update Genre',
            'add_new_item' => 'Add New Genre',
            'new_item_name' => 'New Genre Name',
            'menu_name' => 'Genres',
        ),
        'show_ui' => true,
        'show_admin_column' => true,
        'query_var' => true,
        'rewrite' => array('slug' => 'genre'),
    );
    register_taxonomy('genre', 'movie', $args);
}

add_action('init', 'create_movie_taxonomy');

This adds a Genres taxonomy to your movies, allowing you to assign genres like “Action” or “Drama” to each movie post.

Personal Tip

The first time I worked with custom post types, I got so excited that I went overboard and tried to customize everything at once.

Don’t make the same mistake! Start simple – just create a basic custom post type and then add the extra features as needed. You’ll find that it’s much easier to troubleshoot and refine things step by step.

Wrapping Up

That’s it! You’ve now created a custom post type in WordPress without needing a plugin. Pretty cool, right? By writing this simple code, you’ve gained complete control over your custom content types and avoided the bloat of plugins.

As you get more comfortable with WordPress, you’ll realize that knowing how to create custom post types is one of the most powerful skills you can have in web development.

It opens up so many possibilities – from building a job board, event calendar, product catalog, or anything else your heart desires.

So go ahead, try it out for yourself. If you run into any issues, don’t be afraid to check the WordPress Codex or ask the community. You’ll be creating awesome custom websites in no time!

Happy coding! 😊


Discover more from Prime Inspire

Subscribe to get the latest posts sent to your email.

Leave a Reply

Scroll to Top

Discover more from Prime Inspire

Subscribe now to keep reading and get access to the full archive.

Continue reading