Creating a custom post type in WordPress: A comprehensive guide with PHP examples
Marc Wagner
September 5, 2024
WordPress is known for its flexibility and expandability. Creating a custom post type is also part of this flexibility. These allow you to create specific content in addition to the standard content such as posts and pages. For example, if you are creating a website for movies, you can create a custom post type for movies that is different from normal blog posts.
In this blog article, we’ll show you how to create a custom post type in WordPress using PHP, how to register it and what you need to pay attention to.
What is a custom post type? #
By default, WordPress uses different post types such as posts (post
), pages (page
) and media (attachment
).
With Custom Post Types, you can extend this list to manage custom content, such as portfolios, products, recipes or events.
Creating a custom post type in WordPress #
Prerequisites
Before we start, make sure that you:
- have access to the functions.php file of your theme or use a custom plugin.
- Has basic knowledge of PHP and WordPress.
Step 1: Register custom post type
To create a custom post type, use the WordPress function register_post_type()
.
This function registers the new post type and allows you to define parameters such as name, display and functionality.
PHP example: Custom Post Type “Movies”
Here is an example of how to create a custom post type for movies:
// Custom Post Type für Filme registrieren function cpt_filme() { $labels = array( 'name' => _x( 'Filme', 'post type general name', 'textdomain' ), 'singular_name' => _x( 'Film', 'post type singular name', 'textdomain' ), 'menu_name' => _x( 'Filme', 'admin menu', 'textdomain' ), 'name_admin_bar' => _x( 'Film', 'add new on admin bar', 'textdomain' ), 'add_new' => _x( 'Neuen Film hinzufügen', 'Film', 'textdomain' ), 'add_new_item' => __( 'Neuen Film hinzufügen', 'textdomain' ), 'new_item' => __( 'Neuer Film', 'textdomain' ), 'edit_item' => __( 'Film bearbeiten', 'textdomain' ), 'view_item' => __( 'Film ansehen', 'textdomain' ), 'all_items' => __( 'Alle Filme', 'textdomain' ), 'search_items' => __( 'Filme durchsuchen', 'textdomain' ), 'not_found' => __( 'Keine Filme gefunden', 'textdomain' ), 'not_found_in_trash' => __( 'Keine Filme im Papierkorb gefunden', 'textdomain' ) ); $args = array( 'labels' => $labels, 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_menu' => true, 'query_var' => true, 'rewrite' => array( 'slug' => 'filme' ), 'capability_type' => 'post', 'has_archive' => true, 'hierarchical' => false, 'menu_position' => 5, 'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'comments' ), ); register_post_type( 'filme', $args ); } add_action( 'init', 'cpt_filme' );
Explanation of the code
- Labels: Here you define the labels that are displayed in the backend, e.g. in the admin navigation or when creating a new post.
name
General name for the entire post type group.singular_name
: Singular name used in the user interface.add_new
Name of the button for adding new content.
- Args: These are the arguments that determine how the custom post type works.
public
: Indicates whether the post type is publicly accessible.publicly_queryable
: Makes the post type addressable via the URL.rewrite
: Determines the URL slug (in this case “filme”).supports
Defines the default editor options that are supported for the post type (e.g. title, editor, thumbnail, etc.).
- register_post_type(): This function registers the post type in WordPress.
- add_action(‘init’, ‘cpt_filme’): The post type is registered when WordPress is initialized.
Step 2: Make the custom post type visible in the admin area
Once you have created the post type, it should be visible in the menu in the WordPress dashboard. There you can add and manage new “movies”, similar to normal posts.
Step 3: Customize archive pages and templates
To control the archive pages or individual views of your custom post type, you can create special template files in your theme:
archive-filme.php
This file controls the display of the archive page of the custom post type.single-filme.php
This file controls the display of individual posts of your custom post type.
If these files are not available, WordPress uses the standard templates archive.php
and single.php
.
Bonus: Add taxonomies
To further enhance your custom post type, you can also add custom taxonomies (similar to categories and tags):
function create_filme_taxonomy() { register_taxonomy( 'genre', 'filme', array( 'label' => __( 'Genre' ), 'rewrite' => array( 'slug' => 'genre' ), 'hierarchical' => true, // ähnlich wie Kategorien ) ); } add_action( 'init', 'create_filme_taxonomy' );
Conclusion #
With custom post types, you can customize WordPress to work for any type of website — be it a portfolio website, a blog, an online store or a movie site. The code above gives you the tools to create and customize your own post types.
If you need even more functionality, you can use plugins like “Custom Post Type UI” to further simplify the process.
But with a little PHP knowledge, you can use register_post_type()
to create your own post types according to your needs.
Have fun creating your own custom post type in WordPress!
Artikel von:
Marc Wagner
Hi Marc here. I’m the founder of Forge12 Interactive and have been passionate about building websites, online stores, applications and SaaS solutions for businesses for over 20 years. Before founding the company, I already worked in publicly listed companies and acquired all kinds of knowledge. Now I want to pass this knowledge on to my customers.