Custom post types let you create new content types beyond posts and pages. This helps organize your site better.
Registering custom post types in Wordpress
<?php
function create_custom_post_type() {
register_post_type('custom_type', [
'labels' => [
'name' => __('Custom Types'),
'singular_name' => __('Custom Type')
],
'public' => true,
'has_archive' => true,
'rewrite' => ['slug' => 'custom-types'],
'supports' => ['title', 'editor', 'thumbnail']
]);
}
add_action('init', 'create_custom_post_type');The register_post_type function creates the new type.
Hook it to init so WordPress loads it early.
<?php
function create_portfolio_post_type() {
register_post_type('portfolio', [
'labels' => [
'name' => __('Portfolios'),
'singular_name' => __('Portfolio')
],
'public' => true,
'has_archive' => true,
'supports' => ['title', 'editor', 'thumbnail']
]);
}
add_action('init', 'create_portfolio_post_type');<?php
function create_event_post_type() {
register_post_type('event', [
'labels' => [
'name' => __('Events'),
'singular_name' => __('Event')
],
'public' => true,
'has_archive' => false,
'supports' => ['title', 'editor', 'custom-fields']
]);
}
add_action('init', 'create_event_post_type');This code registers a 'testimonial' post type. It will appear in the WordPress admin menu and supports title and content editing.
<?php
function create_testimonial_post_type() {
register_post_type('testimonial', [
'labels' => [
'name' => __('Testimonials'),
'singular_name' => __('Testimonial')
],
'public' => true,
'has_archive' => true,
'rewrite' => ['slug' => 'testimonials'],
'supports' => ['title', 'editor']
]);
}
add_action('init', 'create_testimonial_post_type');Always use unique slugs to avoid conflicts with existing post types.
Use the 'supports' array to control what editing features your post type has.
Flush rewrite rules by visiting Settings > Permalinks after registering a new post type to avoid 404 errors.
Custom post types help organize different content on your site.
Use register_post_type inside an init hook.
Set labels, visibility, and supported features to customize your post type.
Practice
Solution
Step 1: Understand WordPress content types
WordPress has default content types like posts and pages, but custom post types allow adding new content types.Step 2: Purpose of custom post types
Registering a custom post type lets you organize and display content that doesn't fit posts or pages.Final Answer:
To create a new type of content different from posts and pages -> Option AQuick Check:
Custom post types = new content types [OK]
- Confusing custom post types with themes or plugins
- Thinking it changes user roles
- Assuming it installs plugins automatically
register_post_type function in WordPress?Solution
Step 1: Identify the correct hook for registering post types
Theinithook is the standard place to register custom post types in WordPress.Step 2: Use add_action with 'init'
Usingadd_action('init', 'register_post_type')correctly hooks the function to run at initialization.Final Answer:
add_action('init', 'register_post_type') -> Option AQuick Check:
Hook register_post_type on init [OK]
- Using add_filter instead of add_action
- Hooking into 'wp_head' or 'admin_menu' incorrectly
- Calling register_post_type directly without a hook
add_action('init', function() {
register_post_type('book', [
'label' => 'Books',
'public' => true,
'supports' => ['title', 'editor']
]);
});Solution
Step 1: Analyze the register_post_type parameters
The code registers a post type named 'book' with label 'Books', public visibility, and supports title and editor features.Step 2: Understand the effect of 'public' and 'supports'
Setting 'public' to true makes it visible on the site, and 'supports' enables editing title and content.Final Answer:
Creates a new public post type called 'book' with title and editor support -> Option CQuick Check:
register_post_type with public true and supports = A [OK]
- Confusing post type with taxonomy
- Assuming missing semicolons cause errors in PHP arrays
- Thinking 'public' false means visible
function create_movie_post_type() {
register_post_type('movie', [
'label' => 'Movies',
'public' => true,
'supports' => 'title,editor'
]);
}
add_action('init', 'create_movie_post_type');Solution
Step 1: Check the 'supports' argument type
The 'supports' argument must be an array of features, but here it is a string with comma-separated values.Step 2: Correct the 'supports' argument
It should be ['title', 'editor'] instead of 'title,editor' to work properly.Final Answer:
'supports' should be an array, not a string -> Option DQuick Check:
'supports' requires array format [OK]
- Passing 'supports' as a string instead of array
- Using wrong hook instead of 'init'
- Incorrect function naming conventions
Solution
Step 1: Check 'public' and 'supports' keys
The post type must be public and support title, editor, and custom fields, so 'public' => true and 'supports' => ['title', 'editor', 'custom-fields'] are correct.Step 2: Verify 'menu_icon' value
The icon must be a valid Dashicons class like 'dashicons-calendar'. register_post_type('event', ['public' => true, 'supports' => ['title', 'editor', 'custom-fields'], 'menu_icon' => 'dashicons-calendar']); uses this correctly.Step 3: Identify errors in other options
register_post_type('event', ['public' => false, 'supports' => ['title', 'editor'], 'menu_icon' => 'calendar']); sets 'public' false and wrong icon string; register_post_type('event', ['public' => true, 'supports' => 'title,editor,custom-fields', 'menu_icon' => 'dashicons-calendar']); uses string for 'supports'; register_post_type('event', ['public' => true, 'supports' => ['title', 'editor'], 'menu_icon' => 'dashicons-event']); misses 'custom-fields' support.Final Answer:
register_post_type('event', ['public' => true, 'supports' => ['title', 'editor', 'custom-fields'], 'menu_icon' => 'dashicons-calendar']); -> Option BQuick Check:
Correct keys and values for event post type [OK]
- Using string instead of array for 'supports'
- Setting 'public' to false by mistake
- Using invalid icon names
Ready to prove it?
Pass the test and this pattern moves unknown
