3
* Post API: WP_Post_Type class
11
* Core class used for interacting with post types.
15
* @see register_post_type()
17
final class WP_Post_Type {
28
* Name of the post type shown in the menu. Usually plural.
37
* Labels object for this post type.
39
* If not set, post labels are inherited for non-hierarchical types
40
* and page labels for hierarchical ones.
42
* @see get_post_type_labels()
51
* A short descriptive summary of what the post type is.
57
* @var string $description
59
public $description = '';
62
* Whether a post type is intended for use publicly either via the admin interface or by front-end users.
64
* While the default settings of $exclude_from_search, $publicly_queryable, $show_ui, and $show_in_nav_menus
65
* are inherited from public, each does not rely on this relationship and controls a very specific intention.
73
public $public = false;
76
* Whether the post type is hierarchical (e.g. page).
82
* @var bool $hierarchical
84
public $hierarchical = false;
87
* Whether to exclude posts with this post type from front end search
90
* Default is the opposite value of $public.
94
* @var bool $exclude_from_search
96
public $exclude_from_search = null;
99
* Whether queries can be performed on the front end for the post type as part of `parse_request()`.
101
* Endpoints would include:
102
* - `?post_type={post_type_key}`
103
* - `?{post_type_key}={single_post_slug}`
104
* - `?{post_type_query_var}={single_post_slug}`
106
* Default is the value of $public.
110
* @var bool $publicly_queryable
112
public $publicly_queryable = null;
115
* Whether to generate and allow a UI for managing this post type in the admin.
117
* Default is the value of $public.
123
public $show_ui = null;
126
* Where to show the post type in the admin menu.
128
* To work, $show_ui must be true. If true, the post type is shown in its own top level menu. If false, no menu is
129
* shown. If a string of an existing top level menu (eg. 'tools.php' or 'edit.php?post_type=page'), the post type
130
* will be placed as a sub-menu of that.
132
* Default is the value of $show_ui.
136
* @var bool $show_in_menu
138
public $show_in_menu = null;
141
* Makes this post type available for selection in navigation menus.
143
* Default is the value $public.
147
* @var bool $show_in_nav_menus
149
public $show_in_nav_menus = null;
152
* Makes this post type available via the admin bar.
154
* Default is the value of $show_in_menu.
158
* @var bool $show_in_admin_bar
160
public $show_in_admin_bar = null;
163
* The position in the menu order the post type should appear.
165
* To work, $show_in_menu must be true. Default null (at the bottom).
169
* @var int $menu_position
171
public $menu_position = null;
174
* The URL to the icon to be used for this menu.
176
* Pass a base64-encoded SVG using a data URI, which will be colored to match the color scheme.
177
* This should begin with 'data:image/svg+xml;base64,'. Pass the name of a Dashicons helper class
178
* to use a font icon, e.g. 'dashicons-chart-pie'. Pass 'none' to leave div.wp-menu-image empty
179
* so an icon can be added via CSS.
181
* Defaults to use the posts icon.
185
* @var string $menu_icon
187
public $menu_icon = null;
190
* The string to use to build the read, edit, and delete capabilities.
192
* May be passed as an array to allow for alternative plurals when using
193
* this argument as a base to construct the capabilities, e.g.
194
* array( 'story', 'stories' ). Default 'post'.
198
* @var string $capability_type
200
public $capability_type = 'post';
203
* Whether to use the internal default meta capability handling.
209
* @var bool $map_meta_cap
211
public $map_meta_cap = false;
214
* Provide a callback function that sets up the meta boxes for the edit form.
216
* Do `remove_meta_box()` and `add_meta_box()` calls in the callback. Default null.
220
* @var string $register_meta_box_cb
222
public $register_meta_box_cb = null;
225
* An array of taxonomy identifiers that will be registered for the post type.
227
* Taxonomies can be registered later with `register_taxonomy()` or `register_taxonomy_for_object_type()`.
229
* Default empty array.
233
* @var array $taxonomies
235
public $taxonomies = array();
238
* Whether there should be post type archives, or if a string, the archive slug to use.
240
* Will generate the proper rewrite rules if $rewrite is enabled. Default false.
244
* @var bool|string $has_archive
246
public $has_archive = false;
249
* Sets the query_var key for this post type.
251
* Defaults to $post_type key. If false, a post type cannot be loaded at `?{query_var}={post_slug}`.
252
* If specified as a string, the query `?{query_var_string}={post_slug}` will be valid.
256
* @var string|bool $query_var
261
* Whether to allow this post type to be exported.
267
* @var bool $can_export
269
public $can_export = true;
272
* Whether to delete posts of this type when deleting a user.
274
* If true, posts of this type belonging to the user will be moved to trash when then user is deleted.
275
* If false, posts of this type belonging to the user will *not* be trashed or deleted.
276
* If not set (the default), posts are trashed if post_type_supports( 'author' ).
277
* Otherwise posts are not trashed or deleted. Default null.
281
* @var bool $delete_with_user
283
public $delete_with_user = null;
286
* Whether this post type is a native or "built-in" post_type.
292
* @var bool $_builtin
294
public $_builtin = false;
297
* URL segment to use for edit link of this post type.
299
* Default 'post.php?post=%d'.
303
* @var string $_edit_link
305
public $_edit_link = 'post.php?post=%d';
308
* Post type capabilities.
317
* Triggers the handling of rewrites for this post type.
319
* Defaults to true, using $post_type as slug.
323
* @var array|false $rewrite
328
* The features supported by the post type.
332
* @var array|bool $supports
339
* Will populate object properties from the provided arguments and assign other
340
* default properties based on that information.
345
* @see register_post_type()
347
* @param string $post_type Post type key.
348
* @param array|string $args Optional. Array or string of arguments for registering a post type.
349
* Default empty array.
351
public function __construct( $post_type, $args = array() ) {
352
$this->name = $post_type;
354
$this->set_props( $args );
358
* Sets post type properties.
363
* @param array|string $args Array or string of arguments for registering a post type.
365
public function set_props( $args ) {
366
$args = wp_parse_args( $args );
369
* Filter the arguments for registering a post type.
373
* @param array $args Array of arguments for registering a post type.
374
* @param string $post_type Post type key.
376
$args = apply_filters( 'register_post_type_args', $args, $this->name );
378
$has_edit_link = ! empty( $args['_edit_link'] );
380
// Args prefixed with an underscore are reserved for internal use.
385
'hierarchical' => false,
386
'exclude_from_search' => null,
387
'publicly_queryable' => null,
389
'show_in_menu' => null,
390
'show_in_nav_menus' => null,
391
'show_in_admin_bar' => null,
392
'menu_position' => null,
394
'capability_type' => 'post',
395
'capabilities' => array(),
396
'map_meta_cap' => null,
397
'supports' => array(),
398
'register_meta_box_cb' => null,
399
'taxonomies' => array(),
400
'has_archive' => false,
403
'can_export' => true,
404
'delete_with_user' => null,
406
'_edit_link' => 'post.php?post=%d',
409
$args = array_merge( $defaults, $args );
411
$args['name'] = $this->name;
413
// If not set, default to the setting for public.
414
if ( null === $args['publicly_queryable'] ) {
415
$args['publicly_queryable'] = $args['public'];
418
// If not set, default to the setting for public.
419
if ( null === $args['show_ui'] ) {
420
$args['show_ui'] = $args['public'];
423
// If not set, default to the setting for show_ui.
424
if ( null === $args['show_in_menu'] || ! $args['show_ui'] ) {
425
$args['show_in_menu'] = $args['show_ui'];
428
// If not set, default to the whether the full UI is shown.
429
if ( null === $args['show_in_admin_bar'] ) {
430
$args['show_in_admin_bar'] = (bool) $args['show_in_menu'];
433
// If not set, default to the setting for public.
434
if ( null === $args['show_in_nav_menus'] ) {
435
$args['show_in_nav_menus'] = $args['public'];
438
// If not set, default to true if not public, false if public.
439
if ( null === $args['exclude_from_search'] ) {
440
$args['exclude_from_search'] = ! $args['public'];
443
// Back compat with quirky handling in version 3.0. #14122.
444
if ( empty( $args['capabilities'] ) && null === $args['map_meta_cap'] && in_array( $args['capability_type'], array( 'post', 'page' ) ) ) {
445
$args['map_meta_cap'] = true;
448
// If not set, default to false.
449
if ( null === $args['map_meta_cap'] ) {
450
$args['map_meta_cap'] = false;
453
// If there's no specified edit link and no UI, remove the edit link.
454
if ( ! $args['show_ui'] && ! $has_edit_link ) {
455
$args['_edit_link'] = '';
458
$this->cap = get_post_type_capabilities( (object) $args );
459
unset( $args['capabilities'] );
461
if ( is_array( $args['capability_type'] ) ) {
462
$args['capability_type'] = $args['capability_type'][0];
465
if ( false !== $args['query_var'] ) {
466
if ( true === $args['query_var'] ) {
467
$args['query_var'] = $this->name;
469
$args['query_var'] = sanitize_title_with_dashes( $args['query_var'] );
473
if ( false !== $args['rewrite'] && ( is_admin() || '' != get_option( 'permalink_structure' ) ) ) {
474
if ( ! is_array( $args['rewrite'] ) ) {
475
$args['rewrite'] = array();
477
if ( empty( $args['rewrite']['slug'] ) ) {
478
$args['rewrite']['slug'] = $this->name;
480
if ( ! isset( $args['rewrite']['with_front'] ) ) {
481
$args['rewrite']['with_front'] = true;
483
if ( ! isset( $args['rewrite']['pages'] ) ) {
484
$args['rewrite']['pages'] = true;
486
if ( ! isset( $args['rewrite']['feeds'] ) || ! $args['has_archive'] ) {
487
$args['rewrite']['feeds'] = (bool) $args['has_archive'];
489
if ( ! isset( $args['rewrite']['ep_mask'] ) ) {
490
if ( isset( $args['permalink_epmask'] ) ) {
491
$args['rewrite']['ep_mask'] = $args['permalink_epmask'];
493
$args['rewrite']['ep_mask'] = EP_PERMALINK;
498
foreach ( $args as $property_name => $property_value ) {
499
$this->$property_name = $property_value;
502
$this->labels = get_post_type_labels( $this );
503
$this->label = $this->labels->name;
507
* Sets the features support for the post type.
512
public function add_supports() {
513
if ( ! empty( $this->supports ) ) {
514
add_post_type_support( $this->name, $this->supports );
515
unset( $this->supports );
516
} elseif ( false !== $this->supports ) {
517
// Add default features.
518
add_post_type_support( $this->name, array( 'title', 'editor' ) );
523
* Adds the necessary rewrite rules for the post type.
528
* @global WP_Rewrite $wp_rewrite WordPress Rewrite Component.
529
* @global WP $wp Current WordPress environment instance.
531
public function add_rewrite_rules() {
532
global $wp_rewrite, $wp;
534
if ( false !== $this->query_var && $wp && is_post_type_viewable( $this ) ) {
535
$wp->add_query_var( $this->query_var );
538
if ( false !== $this->rewrite && ( is_admin() || '' != get_option( 'permalink_structure' ) ) ) {
539
if ( $this->hierarchical ) {
540
add_rewrite_tag( "%$this->name%", '(.+?)', $this->query_var ? "{$this->query_var}=" : "post_type=$this->name&pagename=" );
542
add_rewrite_tag( "%$this->name%", '([^/]+)', $this->query_var ? "{$this->query_var}=" : "post_type=$this->name&name=" );
545
if ( $this->has_archive ) {
546
$archive_slug = true === $this->has_archive ? $this->rewrite['slug'] : $this->has_archive;
547
if ( $this->rewrite['with_front'] ) {
548
$archive_slug = substr( $wp_rewrite->front, 1 ) . $archive_slug;
550
$archive_slug = $wp_rewrite->root . $archive_slug;
553
add_rewrite_rule( "{$archive_slug}/?$", "index.php?post_type=$this->name", 'top' );
554
if ( $this->rewrite['feeds'] && $wp_rewrite->feeds ) {
555
$feeds = '(' . trim( implode( '|', $wp_rewrite->feeds ) ) . ')';
556
add_rewrite_rule( "{$archive_slug}/feed/$feeds/?$", "index.php?post_type=$this->name" . '&feed=$matches[1]', 'top' );
557
add_rewrite_rule( "{$archive_slug}/$feeds/?$", "index.php?post_type=$this->name" . '&feed=$matches[1]', 'top' );
559
if ( $this->rewrite['pages'] ) {
560
add_rewrite_rule( "{$archive_slug}/{$wp_rewrite->pagination_base}/([0-9]{1,})/?$", "index.php?post_type=$this->name" . '&paged=$matches[1]', 'top' );
564
$permastruct_args = $this->rewrite;
565
$permastruct_args['feed'] = $permastruct_args['feeds'];
566
add_permastruct( $this->name, "{$this->rewrite['slug']}/%$this->name%", $permastruct_args );
571
* Registers the post type meta box if a custom callback was specified.
576
public function register_meta_boxes() {
577
if ( $this->register_meta_box_cb ) {
578
add_action( 'add_meta_boxes_' . $this->name, $this->register_meta_box_cb, 10, 1 );
583
* Adds the future post hook action for the post type.
588
public function add_hooks() {
589
add_action( 'future_' . $this->name, '_future_post_hook', 5, 2 );
593
* Registers the taxonomies for the post type.
598
public function register_taxonomies() {
599
foreach ( $this->taxonomies as $taxonomy ) {
600
register_taxonomy_for_object_type( $taxonomy, $this->name );
605
* Removes the features support for the post type.
610
* @global array $_wp_post_type_features Post type features.
612
public function remove_supports() {
613
global $_wp_post_type_features;
615
unset( $_wp_post_type_features[ $this->name ] );
619
* Removes any rewrite rules, permastructs, and rules for the post type.
624
* @global WP_Rewrite $wp_rewrite WordPress rewrite component.
625
* @global WP $wp Current WordPress environment instance.
626
* @global array $post_type_meta_caps Used to remove meta capabilities.
628
public function remove_rewrite_rules() {
629
global $wp, $wp_rewrite, $post_type_meta_caps;
632
if ( false !== $this->query_var ) {
633
$wp->remove_query_var( $this->query_var );
636
// Remove any rewrite rules, permastructs, and rules.
637
if ( false !== $this->rewrite ) {
638
remove_rewrite_tag( "%$this->name%" );
639
remove_permastruct( $this->name );
640
foreach ( $wp_rewrite->extra_rules_top as $regex => $query ) {
641
if ( false !== strpos( $query, "index.php?post_type=$this->name" ) ) {
642
unset( $wp_rewrite->extra_rules_top[ $regex ] );
647
// Remove registered custom meta capabilities.
648
foreach ( $this->cap as $cap ) {
649
unset( $post_type_meta_caps[ $cap ] );
654
* Unregisters the post type meta box if a custom callback was specified.
659
public function unregister_meta_boxes() {
660
if ( $this->register_meta_box_cb ) {
661
remove_action( 'add_meta_boxes_' . $this->name, $this->register_meta_box_cb, 10 );
666
* Removes the post type from all taxonomies.
671
public function unregister_taxonomies() {
672
foreach ( get_object_taxonomies( $this->name ) as $taxonomy ) {
673
unregister_taxonomy_for_object_type( $taxonomy, $this->name );
678
* Removes the future post hook action for the post type.
683
public function remove_hooks() {
684
remove_action( 'future_' . $this->name, '_future_post_hook', 5 );