📖 Scenario: You are building a WordPress website for a local art gallery. You want to create a new content type to manage the artworks separately from regular posts.
🎯 Goal: Create a custom post type called artwork with specific arguments to control its behavior and appearance in the WordPress admin and on the site.
📋 What You'll Learn
Create a function called register_artwork_post_type
Use register_post_type to register a post type named artwork
Set the labels argument with name as 'Artworks' and singular_name as 'Artwork'
Set public to true
Set has_archive to true
Set show_in_rest to true to enable Gutenberg editor support
Hook the function to init action
💡 Why This Matters
🌍 Real World
Custom post types let you organize different kinds of content separately, like artworks, events, or products, making your site easier to manage and more user-friendly.
💼 Career
Knowing how to register and configure custom post types is essential for WordPress developers building custom websites or plugins.
Progress0 / 4 steps
1
Create the function and basic post type registration
Create a function called register_artwork_post_type and inside it call register_post_type with the post type name 'artwork' and an empty array for arguments.
Wordpress
Hint
Start by defining the function and call register_post_type with the post type name and empty arguments.
2
Add labels and basic arguments
Inside the register_artwork_post_type function, add an array called $args with a labels key. Set labels to an array with 'name' as 'Artworks' and 'singular_name' as 'Artwork'. Also set public to true and has_archive to true. Pass $args as the second argument to register_post_type.
Wordpress
Hint
Use an array for $args with the required keys and pass it to register_post_type.
3
Enable Gutenberg editor support
Add the show_in_rest argument to the $args array and set it to true to enable the Gutenberg editor for the artwork post type.
Wordpress
Hint
Add 'show_in_rest' => true inside the $args array.
4
Hook the function to the init action
Add a line after the register_artwork_post_type function to hook it to the init action using add_action with the function name 'register_artwork_post_type'.
Wordpress
Hint
Use add_action with 'init' and the function name 'register_artwork_post_type'.
Practice
(1/5)
1. Which argument in register_post_type controls whether the custom post type appears in the WordPress admin menu?
easy
A. public
B. menu_position
C. supports
D. rewrite
Solution
Step 1: Understand the role of menu_position
The menu_position argument sets the location of the custom post type in the WordPress admin menu.
Step 2: Compare with other arguments
public controls visibility on front-end, supports defines features, rewrite controls URL structure.
Final Answer:
menu_position -> Option B
Quick Check:
Admin menu location = menu_position [OK]
Hint: Menu location is set by menu_position argument [OK]
Common Mistakes:
Confusing public with menu visibility
Thinking supports controls menu
Assuming rewrite affects admin menu
2. Which of the following is the correct way to set a custom post type to be publicly queryable in register_post_type arguments?
easy
A. "public_queryable" => true
B. "publiclyqueryable" => true
C. "public_queryable" => "true"
D. "publicly_queryable" => true
Solution
Step 1: Recall the exact argument name
The correct argument is publicly_queryable with an 'ly' and underscore.
Step 2: Check value type and spelling
The value must be boolean true, not string, and spelling must be exact.
Final Answer:
"publicly_queryable" => true -> Option D
Quick Check:
Correct spelling and boolean value = publicly_queryable true [OK]
Hint: Remember 'publicly_queryable' with 'ly' and boolean true [OK]
Common Mistakes:
Misspelling argument name
Using string "true" instead of boolean true
Omitting underscore
3. Given this code snippet, what will be the value of $args['has_archive'] after registration?
A. 'supports' should be an array, not a comma-separated string
B. 'public' should be boolean true, not string 'yes'
C. 'menu_icon' value is invalid, should be a URL
D. No errors, code is correct
Solution
Step 1: Check the supports argument type
supports must be an array of features, not a string.
Step 2: Verify other arguments
public accepts boolean or string 'true'/'false', 'yes' is invalid but PHP may coerce; menu_icon accepts dashicons slug.
Final Answer:
'supports' should be an array, not a comma-separated string -> Option A
Quick Check:
Supports must be array, not string [OK]
Hint: Supports argument must be an array, not a string [OK]
Common Mistakes:
Passing supports as string instead of array
Using invalid string for public
Thinking menu_icon needs URL
5. You want to create a custom post type 'movie' that is public, supports title and editor, has an archive page at '/films', and appears below Posts in the admin menu. Which $args array is correct?
{
'public' => true,
'supports' => ['title', 'editor'],
'has_archive' => true,
'rewrite' => ['slug' => 'films'],
'menu_position' => 5
} sets public to true and supports as an array with 'title' and 'editor', which is correct.
Step 2: Verify archive and rewrite slug
has_archive is true and rewrite slug is 'films' to get archive at '/films'.
Step 3: Confirm menu position
menu_position 5 places it below Posts in admin menu.
Final Answer:
Option A matches all requirements correctly -> Option A