wordpress with custom post types

Welcome dear reader, today you will learn how to extend WordPress with Custom Post Types and Custom Taxonomies. Out of the box, WordPress knows about posts and pages — but what if you run a recipe blog, a real estate site, or a portfolio? Custom Post Types let you model that content properly, without hacking your articles into categories.

At re:Software S.L., we create innovative solutions for businesses that aim to digitize their processes and routines, those often include a WordPress page that we customize to the customers needs!

Table of contents

What are Custom Post Types and Taxonomies?

WordPress stores all content in one database table, tagged with a post type. Posts, pages, attachments — all are post types under the hood. A Custom Post Type (CPT) simply registers a new kind of content, say recipes, with its own admin menu and permalinks.

A taxonomy is a way to group that content. Categories and tags are taxonomies of posts; for your recipes you could register a cuisine taxonomy. Together, CPTs and taxonomies turn WordPress into a full content management system rather than just a blog.

Also, make sure you don’t miss to read our entry on installing WordPress on VPS servers.

How to Register a Custom Post Type

Register your CPT with the register_post_type() function. The best place is a small plugin or your theme’s functions.php:

// Register a "Recipes" custom post type
add_action('init', function () {
    register_post_type('recipe', [
        'labels' => [
            'name'          => 'Recipes',
            'singular_name' => 'Recipe',
        ],
        'public'       => true,
        'has_archive'  => true,
        'menu_icon'    => 'dashicons-food',
        'rewrite'      => ['slug' => 'recipes'],
        'supports'     => ['title', 'editor', 'thumbnail'],
    ]);
});

The key options: public makes it visible, has_archive creates a listing page at /recipes/, menu_icon picks an icon for the admin sidebar, and supports declares which fields the editor shows — here title, content, and a featured image.

How to Register a Custom Taxonomy

Next, attach the cuisine taxonomy to your new post type with register_taxonomy():

// Register a "Cuisine" taxonomy for recipes
add_action('init', function () {
    register_taxonomy('cuisine', 'recipe', [
        'labels' => [
            'name'          => 'Cuisines',
            'singular_name' => 'Cuisine',
        ],
        'hierarchical' => true,
        'show_in_rest' => true,
    ]);
});

With hierarchical set to true, cuisines work like categories with parents and children; set it to false for free-form labels like tags. The show_in_rest flag makes the taxonomy available to the block editor and the REST API.

Where Do My Custom Posts Appear?

After adding a few recipes, your content lives at three places: the archive at yoursite.com/recipes/, individual pages at yoursite.com/recipes/pizza-margherita/, and a new Recipes menu in the admin sidebar. WordPress generates the templates automatically — archive-recipe.php and single-recipe.php in your theme will override them when you are ready for custom design.

One tip: after editing the rewrite rules, visit Settings -> Permalinks and click Save once to flush the rules, otherwise you may see 404 pages.

Conclusion

That’s already it to create your first Custom Post Type and Taxonomy in WordPress. Register a post type for your content, attach a taxonomy to organize it, and let WordPress handle the URLs and admin UI. Leave a comment below with the content types you built for your site!

By Editorial Team

A source code library for the open world! Find snippets and examples for popular programming languages including PHP, Javascript, Typescript, Node.js, Vue.js, and more.

Leave a Reply