WP_Post_Type

Core class used for interacting with post types.

Defined (1)

The class is defined in the following location(s).

/wp-includes/class-wp-post-type.php  
  1. final class WP_Post_Type { 
  2. /** 
  3. * Post type key. 
  4. * @since 4.6.0 
  5. * @access public 
  6. * @var string $name 
  7. */ 
  8. public $name; 
  9.  
  10. /** 
  11. * Name of the post type shown in the menu. Usually plural. 
  12. * @since 4.6.0 
  13. * @access public 
  14. * @var string $label 
  15. */ 
  16. public $label; 
  17.  
  18. /** 
  19. * Labels object for this post type. 
  20. * If not set, post labels are inherited for non-hierarchical types 
  21. * and page labels for hierarchical ones. 
  22. * @see get_post_type_labels() 
  23. * @since 4.6.0 
  24. * @access public 
  25. * @var object $labels 
  26. */ 
  27. public $labels; 
  28.  
  29. /** 
  30. * A short descriptive summary of what the post type is. 
  31. * Default empty. 
  32. * @since 4.6.0 
  33. * @access public 
  34. * @var string $description 
  35. */ 
  36. public $description = ''; 
  37.  
  38. /** 
  39. * Whether a post type is intended for use publicly either via the admin interface or by front-end users. 
  40. * While the default settings of $exclude_from_search, $publicly_queryable, $show_ui, and $show_in_nav_menus 
  41. * are inherited from public, each does not rely on this relationship and controls a very specific intention. 
  42. * Default false. 
  43. * @since 4.6.0 
  44. * @access public 
  45. * @var bool $public 
  46. */ 
  47. public $public = false; 
  48.  
  49. /** 
  50. * Whether the post type is hierarchical (e.g. page). 
  51. * Default false. 
  52. * @since 4.6.0 
  53. * @access public 
  54. * @var bool $hierarchical 
  55. */ 
  56. public $hierarchical = false; 
  57.  
  58. /** 
  59. * Whether to exclude posts with this post type from front end search 
  60. * results. 
  61. * Default is the opposite value of $public. 
  62. * @since 4.6.0 
  63. * @access public 
  64. * @var bool $exclude_from_search 
  65. */ 
  66. public $exclude_from_search = null; 
  67.  
  68. /** 
  69. * Whether queries can be performed on the front end for the post type as part of `parse_request()`. 
  70. * Endpoints would include: 
  71. * - `?post_type={post_type_key}` 
  72. * - `?{post_type_key}={single_post_slug}` 
  73. * - `?{post_type_query_var}={single_post_slug}` 
  74. * Default is the value of $public. 
  75. * @since 4.6.0 
  76. * @access public 
  77. * @var bool $publicly_queryable 
  78. */ 
  79. public $publicly_queryable = null; 
  80.  
  81. /** 
  82. * Whether to generate and allow a UI for managing this post type in the admin. 
  83. * Default is the value of $public. 
  84. * @since 4.6.0 
  85. * @access public 
  86. * @var bool $show_ui 
  87. */ 
  88. public $show_ui = null; 
  89.  
  90. /** 
  91. * Where to show the post type in the admin menu. 
  92. * 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 
  93. * shown. If a string of an existing top level menu (eg. 'tools.php' or 'edit.php?post_type=page'), the post type 
  94. * will be placed as a sub-menu of that. 
  95. * Default is the value of $show_ui. 
  96. * @since 4.6.0 
  97. * @access public 
  98. * @var bool $show_in_menu 
  99. */ 
  100. public $show_in_menu = null; 
  101.  
  102. /** 
  103. * Makes this post type available for selection in navigation menus. 
  104. * Default is the value $public. 
  105. * @since 4.6.0 
  106. * @access public 
  107. * @var bool $show_in_nav_menus 
  108. */ 
  109. public $show_in_nav_menus = null; 
  110.  
  111. /** 
  112. * Makes this post type available via the admin bar. 
  113. * Default is the value of $show_in_menu. 
  114. * @since 4.6.0 
  115. * @access public 
  116. * @var bool $show_in_admin_bar 
  117. */ 
  118. public $show_in_admin_bar = null; 
  119.  
  120. /** 
  121. * The position in the menu order the post type should appear. 
  122. * To work, $show_in_menu must be true. Default null (at the bottom). 
  123. * @since 4.6.0 
  124. * @access public 
  125. * @var int $menu_position 
  126. */ 
  127. public $menu_position = null; 
  128.  
  129. /** 
  130. * The URL to the icon to be used for this menu. 
  131. * Pass a base64-encoded SVG using a data URI, which will be colored to match the color scheme. 
  132. * This should begin with 'data:image/svg+xml;base64, '. Pass the name of a Dashicons helper class 
  133. * to use a font icon, e.g. 'dashicons-chart-pie'. Pass 'none' to leave div.wp-menu-image empty 
  134. * so an icon can be added via CSS. 
  135. * Defaults to use the posts icon. 
  136. * @since 4.6.0 
  137. * @access public 
  138. * @var string $menu_icon 
  139. */ 
  140. public $menu_icon = null; 
  141.  
  142. /** 
  143. * The string to use to build the read, edit, and delete capabilities. 
  144. * May be passed as an array to allow for alternative plurals when using 
  145. * this argument as a base to construct the capabilities, e.g. 
  146. * array( 'story', 'stories' ). Default 'post'. 
  147. * @since 4.6.0 
  148. * @access public 
  149. * @var string $capability_type 
  150. */ 
  151. public $capability_type = 'post'; 
  152.  
  153. /** 
  154. * Whether to use the internal default meta capability handling. 
  155. * Default false. 
  156. * @since 4.6.0 
  157. * @access public 
  158. * @var bool $map_meta_cap 
  159. */ 
  160. public $map_meta_cap = false; 
  161.  
  162. /** 
  163. * Provide a callback function that sets up the meta boxes for the edit form. 
  164. * Do `remove_meta_box()` and `add_meta_box()` calls in the callback. Default null. 
  165. * @since 4.6.0 
  166. * @access public 
  167. * @var string $register_meta_box_cb 
  168. */ 
  169. public $register_meta_box_cb = null; 
  170.  
  171. /** 
  172. * An array of taxonomy identifiers that will be registered for the post type. 
  173. * Taxonomies can be registered later with `register_taxonomy()` or `register_taxonomy_for_object_type()`. 
  174. * Default empty array. 
  175. * @since 4.6.0 
  176. * @access public 
  177. * @var array $taxonomies 
  178. */ 
  179. public $taxonomies = array(); 
  180.  
  181. /** 
  182. * Whether there should be post type archives, or if a string, the archive slug to use. 
  183. * Will generate the proper rewrite rules if $rewrite is enabled. Default false. 
  184. * @since 4.6.0 
  185. * @access public 
  186. * @var bool|string $has_archive 
  187. */ 
  188. public $has_archive = false; 
  189.  
  190. /** 
  191. * Sets the query_var key for this post type. 
  192. * Defaults to $post_type key. If false, a post type cannot be loaded at `?{query_var}={post_slug}`. 
  193. * If specified as a string, the query `?{query_var_string}={post_slug}` will be valid. 
  194. * @since 4.6.0 
  195. * @access public 
  196. * @var string|bool $query_var 
  197. */ 
  198. public $query_var; 
  199.  
  200. /** 
  201. * Whether to allow this post type to be exported. 
  202. * Default true. 
  203. * @since 4.6.0 
  204. * @access public 
  205. * @var bool $can_export 
  206. */ 
  207. public $can_export = true; 
  208.  
  209. /** 
  210. * Whether to delete posts of this type when deleting a user. 
  211. * If true, posts of this type belonging to the user will be moved to trash when then user is deleted. 
  212. * If false, posts of this type belonging to the user will *not* be trashed or deleted. 
  213. * If not set (the default), posts are trashed if post_type_supports( 'author' ). 
  214. * Otherwise posts are not trashed or deleted. Default null. 
  215. * @since 4.6.0 
  216. * @access public 
  217. * @var bool $delete_with_user 
  218. */ 
  219. public $delete_with_user = null; 
  220.  
  221. /** 
  222. * Whether this post type is a native or "built-in" post_type. 
  223. * Default false. 
  224. * @since 4.6.0 
  225. * @access public 
  226. * @var bool $_builtin 
  227. */ 
  228. public $_builtin = false; 
  229.  
  230. /** 
  231. * URL segment to use for edit link of this post type. 
  232. * Default 'post.php?post=%d'. 
  233. * @since 4.6.0 
  234. * @access public 
  235. * @var string $_edit_link 
  236. */ 
  237. public $_edit_link = 'post.php?post=%d'; 
  238.  
  239. /** 
  240. * Post type capabilities. 
  241. * @since 4.6.0 
  242. * @access public 
  243. * @var object $cap 
  244. */ 
  245. public $cap; 
  246.  
  247. /** 
  248. * Triggers the handling of rewrites for this post type. 
  249. * Defaults to true, using $post_type as slug. 
  250. * @since 4.6.0 
  251. * @access public 
  252. * @var array|false $rewrite 
  253. */ 
  254. public $rewrite; 
  255.  
  256. /** 
  257. * The features supported by the post type. 
  258. * @since 4.6.0 
  259. * @access public 
  260. * @var array|bool $supports 
  261. */ 
  262. public $supports; 
  263.  
  264. /** 
  265. * Whether this post type should appear in the REST API. 
  266. * Default false. If true, standard endpoints will be registered with 
  267. * respect to $rest_base and $rest_controller_class. 
  268. * @since 4.7.4 
  269. * @access public 
  270. * @var bool $show_in_rest 
  271. */ 
  272. public $show_in_rest; 
  273.  
  274. /** 
  275. * The base path for this post type's REST API endpoints. 
  276. * @since 4.7.4 
  277. * @access public 
  278. * @var string|bool $rest_base 
  279. */ 
  280. public $rest_base; 
  281.  
  282. /** 
  283. * The controller for this post type's REST API endpoints. 
  284. * Custom controllers must extend WP_REST_Controller. 
  285. * @since 4.7.4 
  286. * @access public 
  287. * @var string|bool $rest_controller_class 
  288. */ 
  289. public $rest_controller_class; 
  290.  
  291. /** 
  292. * Constructor. 
  293. * Will populate object properties from the provided arguments and assign other 
  294. * default properties based on that information. 
  295. * @since 4.6.0 
  296. * @access public 
  297. * @see register_post_type() 
  298. * @param string $post_type Post type key. 
  299. * @param array|string $args Optional. Array or string of arguments for registering a post type. 
  300. * Default empty array. 
  301. */ 
  302. public function __construct( $post_type, $args = array() ) { 
  303. $this->name = $post_type; 
  304.  
  305. $this->set_props( $args ); 
  306.  
  307. /** 
  308. * Sets post type properties. 
  309. * @since 4.6.0 
  310. * @access public 
  311. * @param array|string $args Array or string of arguments for registering a post type. 
  312. */ 
  313. public function set_props( $args ) { 
  314. $args = wp_parse_args( $args ); 
  315.  
  316. /** 
  317. * Filters the arguments for registering a post type. 
  318. * @since 4.4.0 
  319. * @param array $args Array of arguments for registering a post type. 
  320. * @param string $post_type Post type key. 
  321. */ 
  322. $args = apply_filters( 'register_post_type_args', $args, $this->name ); 
  323.  
  324. $has_edit_link = ! empty( $args['_edit_link'] ); 
  325.  
  326. // Args prefixed with an underscore are reserved for internal use. 
  327. $defaults = array( 
  328. 'labels' => array(),  
  329. 'description' => '',  
  330. 'public' => false,  
  331. 'hierarchical' => false,  
  332. 'exclude_from_search' => null,  
  333. 'publicly_queryable' => null,  
  334. 'show_ui' => null,  
  335. 'show_in_menu' => null,  
  336. 'show_in_nav_menus' => null,  
  337. 'show_in_admin_bar' => null,  
  338. 'menu_position' => null,  
  339. 'menu_icon' => null,  
  340. 'capability_type' => 'post',  
  341. 'capabilities' => array(),  
  342. 'map_meta_cap' => null,  
  343. 'supports' => array(),  
  344. 'register_meta_box_cb' => null,  
  345. 'taxonomies' => array(),  
  346. 'has_archive' => false,  
  347. 'rewrite' => true,  
  348. 'query_var' => true,  
  349. 'can_export' => true,  
  350. 'delete_with_user' => null,  
  351. 'show_in_rest' => false,  
  352. 'rest_base' => false,  
  353. 'rest_controller_class' => false,  
  354. '_builtin' => false,  
  355. '_edit_link' => 'post.php?post=%d',  
  356. ); 
  357.  
  358. $args = array_merge( $defaults, $args ); 
  359.  
  360. $args['name'] = $this->name; 
  361.  
  362. // If not set, default to the setting for public. 
  363. if ( null === $args['publicly_queryable'] ) { 
  364. $args['publicly_queryable'] = $args['public']; 
  365.  
  366. // If not set, default to the setting for public. 
  367. if ( null === $args['show_ui'] ) { 
  368. $args['show_ui'] = $args['public']; 
  369.  
  370. // If not set, default to the setting for show_ui. 
  371. if ( null === $args['show_in_menu'] || ! $args['show_ui'] ) { 
  372. $args['show_in_menu'] = $args['show_ui']; 
  373.  
  374. // If not set, default to the whether the full UI is shown. 
  375. if ( null === $args['show_in_admin_bar'] ) { 
  376. $args['show_in_admin_bar'] = (bool) $args['show_in_menu']; 
  377.  
  378. // If not set, default to the setting for public. 
  379. if ( null === $args['show_in_nav_menus'] ) { 
  380. $args['show_in_nav_menus'] = $args['public']; 
  381.  
  382. // If not set, default to true if not public, false if public. 
  383. if ( null === $args['exclude_from_search'] ) { 
  384. $args['exclude_from_search'] = ! $args['public']; 
  385.  
  386. // Back compat with quirky handling in version 3.0. #14122. 
  387. if ( empty( $args['capabilities'] ) && null === $args['map_meta_cap'] && in_array( $args['capability_type'], array( 'post', 'page' ) ) ) { 
  388. $args['map_meta_cap'] = true; 
  389.  
  390. // If not set, default to false. 
  391. if ( null === $args['map_meta_cap'] ) { 
  392. $args['map_meta_cap'] = false; 
  393.  
  394. // If there's no specified edit link and no UI, remove the edit link. 
  395. if ( ! $args['show_ui'] && ! $has_edit_link ) { 
  396. $args['_edit_link'] = ''; 
  397.  
  398. $this->cap = get_post_type_capabilities( (object) $args ); 
  399. unset( $args['capabilities'] ); 
  400.  
  401. if ( is_array( $args['capability_type'] ) ) { 
  402. $args['capability_type'] = $args['capability_type'][0]; 
  403.  
  404. if ( false !== $args['query_var'] ) { 
  405. if ( true === $args['query_var'] ) { 
  406. $args['query_var'] = $this->name; 
  407. } else { 
  408. $args['query_var'] = sanitize_title_with_dashes( $args['query_var'] ); 
  409.  
  410. if ( false !== $args['rewrite'] && ( is_admin() || '' != get_option( 'permalink_structure' ) ) ) { 
  411. if ( ! is_array( $args['rewrite'] ) ) { 
  412. $args['rewrite'] = array(); 
  413. if ( empty( $args['rewrite']['slug'] ) ) { 
  414. $args['rewrite']['slug'] = $this->name; 
  415. if ( ! isset( $args['rewrite']['with_front'] ) ) { 
  416. $args['rewrite']['with_front'] = true; 
  417. if ( ! isset( $args['rewrite']['pages'] ) ) { 
  418. $args['rewrite']['pages'] = true; 
  419. if ( ! isset( $args['rewrite']['feeds'] ) || ! $args['has_archive'] ) { 
  420. $args['rewrite']['feeds'] = (bool) $args['has_archive']; 
  421. if ( ! isset( $args['rewrite']['ep_mask'] ) ) { 
  422. if ( isset( $args['permalink_epmask'] ) ) { 
  423. $args['rewrite']['ep_mask'] = $args['permalink_epmask']; 
  424. } else { 
  425. $args['rewrite']['ep_mask'] = EP_PERMALINK; 
  426.  
  427. foreach ( $args as $property_name => $property_value ) { 
  428. $this->$property_name = $property_value; 
  429.  
  430. $this->labels = get_post_type_labels( $this ); 
  431. $this->label = $this->labels->name; 
  432.  
  433. /** 
  434. * Sets the features support for the post type. 
  435. * @since 4.6.0 
  436. * @access public 
  437. */ 
  438. public function add_supports() { 
  439. if ( ! empty( $this->supports ) ) { 
  440. add_post_type_support( $this->name, $this->supports ); 
  441. unset( $this->supports ); 
  442. } elseif ( false !== $this->supports ) { 
  443. // Add default features. 
  444. add_post_type_support( $this->name, array( 'title', 'editor' ) ); 
  445.  
  446. /** 
  447. * Adds the necessary rewrite rules for the post type. 
  448. * @since 4.6.0 
  449. * @access public 
  450. * @global WP_Rewrite $wp_rewrite WordPress Rewrite Component. 
  451. * @global WP $wp Current WordPress environment instance. 
  452. */ 
  453. public function add_rewrite_rules() { 
  454. global $wp_rewrite, $wp; 
  455.  
  456. if ( false !== $this->query_var && $wp && is_post_type_viewable( $this ) ) { 
  457. $wp->add_query_var( $this->query_var ); 
  458.  
  459. if ( false !== $this->rewrite && ( is_admin() || '' != get_option( 'permalink_structure' ) ) ) { 
  460. if ( $this->hierarchical ) { 
  461. add_rewrite_tag( "%$this->name%", '(.+?)', $this->query_var ? "{$this->query_var}=" : "post_type=$this->name&pagename=" ); 
  462. } else { 
  463. add_rewrite_tag( "%$this->name%", '([^/]+)', $this->query_var ? "{$this->query_var}=" : "post_type=$this->name&name=" ); 
  464.  
  465. if ( $this->has_archive ) { 
  466. $archive_slug = true === $this->has_archive ? $this->rewrite['slug'] : $this->has_archive; 
  467. if ( $this->rewrite['with_front'] ) { 
  468. $archive_slug = substr( $wp_rewrite->front, 1 ) . $archive_slug; 
  469. } else { 
  470. $archive_slug = $wp_rewrite->root . $archive_slug; 
  471.  
  472. add_rewrite_rule( "{$archive_slug}/?$", "index.php?post_type=$this->name", 'top' ); 
  473. if ( $this->rewrite['feeds'] && $wp_rewrite->feeds ) { 
  474. $feeds = '(' . trim( implode( '|', $wp_rewrite->feeds ) ) . ')'; 
  475. add_rewrite_rule( "{$archive_slug}/feed/$feeds/?$", "index.php?post_type=$this->name" . '&feed=$matches[1]', 'top' ); 
  476. add_rewrite_rule( "{$archive_slug}/$feeds/?$", "index.php?post_type=$this->name" . '&feed=$matches[1]', 'top' ); 
  477. if ( $this->rewrite['pages'] ) { 
  478. add_rewrite_rule( "{$archive_slug}/{$wp_rewrite->pagination_base}/([0-9]{1, })/?$", "index.php?post_type=$this->name" . '&paged=$matches[1]', 'top' ); 
  479.  
  480. $permastruct_args = $this->rewrite; 
  481. $permastruct_args['feed'] = $permastruct_args['feeds']; 
  482. add_permastruct( $this->name, "{$this->rewrite['slug']}/%$this->name%", $permastruct_args ); 
  483.  
  484. /** 
  485. * Registers the post type meta box if a custom callback was specified. 
  486. * @since 4.6.0 
  487. * @access public 
  488. */ 
  489. public function register_meta_boxes() { 
  490. if ( $this->register_meta_box_cb ) { 
  491. add_action( 'add_meta_boxes_' . $this->name, $this->register_meta_box_cb, 10, 1 ); 
  492.  
  493. /** 
  494. * Adds the future post hook action for the post type. 
  495. * @since 4.6.0 
  496. * @access public 
  497. */ 
  498. public function add_hooks() { 
  499. add_action( 'future_' . $this->name, '_future_post_hook', 5, 2 ); 
  500.  
  501. /** 
  502. * Registers the taxonomies for the post type. 
  503. * @since 4.6.0 
  504. * @access public 
  505. */ 
  506. public function register_taxonomies() { 
  507. foreach ( $this->taxonomies as $taxonomy ) { 
  508. register_taxonomy_for_object_type( $taxonomy, $this->name ); 
  509.  
  510. /** 
  511. * Removes the features support for the post type. 
  512. * @since 4.6.0 
  513. * @access public 
  514. * @global array $_wp_post_type_features Post type features. 
  515. */ 
  516. public function remove_supports() { 
  517. global $_wp_post_type_features; 
  518.  
  519. unset( $_wp_post_type_features[ $this->name ] ); 
  520.  
  521. /** 
  522. * Removes any rewrite rules, permastructs, and rules for the post type. 
  523. * @since 4.6.0 
  524. * @access public 
  525. * @global WP_Rewrite $wp_rewrite WordPress rewrite component. 
  526. * @global WP $wp Current WordPress environment instance. 
  527. * @global array $post_type_meta_caps Used to remove meta capabilities. 
  528. */ 
  529. public function remove_rewrite_rules() { 
  530. global $wp, $wp_rewrite, $post_type_meta_caps; 
  531.  
  532. // Remove query var. 
  533. if ( false !== $this->query_var ) { 
  534. $wp->remove_query_var( $this->query_var ); 
  535.  
  536. // Remove any rewrite rules, permastructs, and rules. 
  537. if ( false !== $this->rewrite ) { 
  538. remove_rewrite_tag( "%$this->name%" ); 
  539. remove_permastruct( $this->name ); 
  540. foreach ( $wp_rewrite->extra_rules_top as $regex => $query ) { 
  541. if ( false !== strpos( $query, "index.php?post_type=$this->name" ) ) { 
  542. unset( $wp_rewrite->extra_rules_top[ $regex ] ); 
  543.  
  544. // Remove registered custom meta capabilities. 
  545. foreach ( $this->cap as $cap ) { 
  546. unset( $post_type_meta_caps[ $cap ] ); 
  547.  
  548. /** 
  549. * Unregisters the post type meta box if a custom callback was specified. 
  550. * @since 4.6.0 
  551. * @access public 
  552. */ 
  553. public function unregister_meta_boxes() { 
  554. if ( $this->register_meta_box_cb ) { 
  555. remove_action( 'add_meta_boxes_' . $this->name, $this->register_meta_box_cb, 10 ); 
  556.  
  557. /** 
  558. * Removes the post type from all taxonomies. 
  559. * @since 4.6.0 
  560. * @access public 
  561. */ 
  562. public function unregister_taxonomies() { 
  563. foreach ( get_object_taxonomies( $this->name ) as $taxonomy ) { 
  564. unregister_taxonomy_for_object_type( $taxonomy, $this->name ); 
  565.  
  566. /** 
  567. * Removes the future post hook action for the post type. 
  568. * @since 4.6.0 
  569. * @access public 
  570. */ 
  571. public function remove_hooks() { 
  572. remove_action( 'future_' . $this->name, '_future_post_hook', 5 );