register_post_type()
Проверено на WordPress 6.9, обновлено Источник: WordPress Developer Resources.
Сигнатура
register_post_type( string $post_type, array|string $args = array() ): WP_Post_Type|WP_Error
Описание
Примечание: регистрацию типов записей не следует подключать до действия ‘init’. Кроме того, любые связи с таксономиями следует регистрировать через аргумент $taxonomies, чтобы обеспечить согласованность при использовании таких хуков, как ‘parse_query’ или ‘pre_get_posts’.
Типы записей могут поддерживать любое количество встроенных возможностей ядра, таких как метабоксы, произвольные поля, миниатюры записей, статусы записей, комментарии и другое. Полный список поддерживаемых возможностей см. в аргументе $supports.
Оригинал (английский)
Note: Post type registrations should not be hooked before the ‘init’ action. Also, any taxonomy connections should be registered via the $taxonomies argument to ensure consistency when hooks such as ‘parse_query’ or ‘pre_get_posts’ are used.
Post types can support any number of built-in core features such as meta boxes, custom fields, post thumbnails, post statuses, comments, and more. See the $supports argument for a complete list of supported features.
Параметры
$post_type
string
обязательный
Возвращаемое значение
WP_Post_Type|WP_Error
Исходный код
wp-includes/post.php:1818
function register_post_type( $post_type, $args = array() ) {
global $wp_post_types;
if ( ! is_array( $wp_post_types ) ) {
$wp_post_types = array();
}
// Sanitize post type name.
$post_type = sanitize_key( $post_type );
if ( empty( $post_type ) || strlen( $post_type ) > 20 ) {
_doing_it_wrong( __FUNCTION__, __( 'Post type names must be between 1 and 20 characters in length.' ), '4.2.0' );
return new WP_Error( 'post_type_length_invalid', __( 'Post type names must be between 1 and 20 characters in length.' ) );
}
$post_type_object = new WP_Post_Type( $post_type, $args );
$post_type_object->add_supports();
$post_type_object->add_rewrite_rules();
$post_type_object->register_meta_boxes();
$wp_post_types[ $post_type ] = $post_type_object;
$post_type_object->add_hooks();
$post_type_object->register_taxonomies();
/**
* Fires after a post type is registered.
*
* @since 3.3.0
* @since 4.6.0 Converted the `$post_type` parameter to accept a WP_Post_Type object.
*
* @param string $post_type Post type.
* @param WP_Post_Type $post_type_object Arguments used to register the post type.
*/
do_action( 'registered_post_type', $post_type, $post_type_object );
/**
* Fires after a specific post type is registered.
*
* The dynamic portion of the filter name, `$post_type`, refers to the post type key.
*
* Possible hook names include:
*
* - `registered_post_type_post`
* - `registered_post_type_page`
*
* @since 6.0.0
*
* @param string $post_type Post type.
* @param WP_Post_Type $post_type_object Arguments used to register the post type.
*/
do_action( "registered_post_type_{$post_type}", $post_type, $post_type_object );
return $post_type_object;
}
История изменений
| Версия | Описание |
|---|---|
| 5.9.0 | The rest_namespace argument was added. |
| 5.3.0 | The supports argument will now accept an array of arguments for a feature. |
| 5.0.0 | The template and template_lock arguments were added. |
| 4.7.0 | Introduced show_in_rest, rest_base and rest_controller_class arguments to register the post type in REST API. |
| 4.6.0 | Post type object returned is now an instance of WP_Post_Type. |
| 4.4.0 | The show_ui argument is now enforced on the post type listing screen and post editing screen. |
| 3.0.0 | The show_ui argument is now enforced on the new post screen. |
| 2.9.0 | Introduced. |

