0
0
Wordpressframework~5 mins

Custom post type arguments in Wordpress

Choose your learning style9 modes available
Introduction

Custom post type arguments tell WordPress how to create and show your new content type. They control what features it has and how it behaves.

You want to add a new content type like 'Books' or 'Recipes' to your website.
You need to control if your content type appears in menus or search results.
You want to decide what editing features are available for your custom content.
You want to set custom labels and icons for your content type in the admin area.
Syntax
Wordpress
register_post_type('post_type_name', [
  'labels' => [
    'name' => 'Plural Name',
    'singular_name' => 'Singular Name'
  ],
  'public' => true,
  'has_archive' => true,
  'supports' => ['title', 'editor', 'thumbnail'],
  'menu_icon' => 'dashicons-admin-post'
]);

The first argument is the unique name for your post type (lowercase, no spaces).

The second argument is an array of options that control how the post type works and looks.

Examples
Creates a public 'book' post type with archive pages enabled.
Wordpress
register_post_type('book', [
  'labels' => [
    'name' => 'Books',
    'singular_name' => 'Book'
  ],
  'public' => true,
  'has_archive' => true
]);
Creates a 'movie' post type that is not public but visible in the admin area with title and editor support.
Wordpress
register_post_type('movie', [
  'labels' => [
    'name' => 'Movies',
    'singular_name' => 'Movie'
  ],
  'public' => false,
  'show_ui' => true,
  'supports' => ['title', 'editor']
]);
Creates a 'recipe' post type with a carrot icon and supports comments and thumbnails.
Wordpress
register_post_type('recipe', [
  'labels' => [
    'name' => 'Recipes',
    'singular_name' => 'Recipe'
  ],
  'public' => true,
  'menu_icon' => 'dashicons-carrot',
  'supports' => ['title', 'editor', 'thumbnail', 'comments']
]);
Sample Program

This code creates a new post type called 'event' that is public, has archive pages, supports title, editor, and thumbnail, and uses a calendar icon in the admin menu.

Wordpress
<?php
function create_custom_post_type() {
  register_post_type('event', [
    'labels' => [
      'name' => 'Events',
      'singular_name' => 'Event'
    ],
    'public' => true,
    'has_archive' => true,
    'supports' => ['title', 'editor', 'thumbnail'],
    'menu_icon' => 'dashicons-calendar-alt'
  ]);
}
add_action('init', 'create_custom_post_type');
OutputSuccess
Important Notes

Always hook your custom post type registration to the 'init' action to ensure WordPress is ready.

Use descriptive labels to make the admin area user-friendly.

Setting 'public' to false hides the post type from the front end but can still show it in admin if 'show_ui' is true.

Summary

Custom post type arguments define how your new content type behaves and looks.

Use the 'labels', 'public', 'supports', and 'menu_icon' keys to customize your post type.

Register your post type inside a function hooked to 'init' for proper loading.