A WordPress-centric search engine for devs and theme authors



register_post_type ›

Since2.9.0
Deprecatedn/a
register_post_type ( $post_type, $args = array() )
Parameters: (2)
  • (string) $post_type Post type key. Must not exceed 20 characters and may only contain lowercase alphanumeric characters, dashes, and underscores. See sanitize_key().
    Required: Yes
  • (array|string) $args { Array or string of arguments for registering a post type.
    Required: No
    Default: array()
Returns:
  • (WP_Post_Type|WP_Error) The registered post type object, or an error object.
Defined at:
Codex:
Change Log:
  • 3.0.0
  • 4.4.0
  • 4.6.0
  • 4.7.0

Registers a post type.

Note: Post type registrations should not be hooked before the {@see 'init'} action. Also, any taxonomy connections should be registered via the $taxonomies argument to ensure consistency when hooks such as {@see 'parse_query'} or {@see '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.



Source

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 );

	return $post_type_object;
}