WP_Post_Type::set_props()publicWP 4.6.0

Sets post type properties.

See the register_post_type() function for accepted arguments for $args.

Method of the class: WP_Post_Type{}

Return

null. Nothing (null).

Usage

$WP_Post_Type = new WP_Post_Type();
$WP_Post_Type->set_props( $args );
$args(array|string) (required)
Array or string of arguments for registering a post type.

Changelog

Since 4.6.0 Introduced.

WP_Post_Type::set_props() code WP 6.4.3

public function set_props( $args ) {
	$args = wp_parse_args( $args );

	/**
	 * Filters the arguments for registering a post type.
	 *
	 * @since 4.4.0
	 *
	 * @param array  $args      Array of arguments for registering a post type.
	 *                          See the register_post_type() function for accepted arguments.
	 * @param string $post_type Post type key.
	 */
	$args = apply_filters( 'register_post_type_args', $args, $this->name );

	$post_type = $this->name;

	/**
	 * Filters the arguments for registering a specific post type.
	 *
	 * The dynamic portion of the filter name, `$post_type`, refers to the post type key.
	 *
	 * Possible hook names include:
	 *
	 *  - `register_post_post_type_args`
	 *  - `register_page_post_type_args`
	 *
	 * @since 6.0.0
	 * @since 6.4.0 Added `late_route_registration`, `autosave_rest_controller_class` and `revisions_rest_controller_class` arguments.
	 *
	 * @param array  $args      Array of arguments for registering a post type.
	 *                          See the register_post_type() function for accepted arguments.
	 * @param string $post_type Post type key.
	 */
	$args = apply_filters( "register_{$post_type}_post_type_args", $args, $this->name );

	$has_edit_link = ! empty( $args['_edit_link'] );

	// Args prefixed with an underscore are reserved for internal use.
	$defaults = array(
		'labels'                          => array(),
		'description'                     => '',
		'public'                          => false,
		'hierarchical'                    => false,
		'exclude_from_search'             => null,
		'publicly_queryable'              => null,
		'show_ui'                         => null,
		'show_in_menu'                    => null,
		'show_in_nav_menus'               => null,
		'show_in_admin_bar'               => null,
		'menu_position'                   => null,
		'menu_icon'                       => null,
		'capability_type'                 => 'post',
		'capabilities'                    => array(),
		'map_meta_cap'                    => null,
		'supports'                        => array(),
		'register_meta_box_cb'            => null,
		'taxonomies'                      => array(),
		'has_archive'                     => false,
		'rewrite'                         => true,
		'query_var'                       => true,
		'can_export'                      => true,
		'delete_with_user'                => null,
		'show_in_rest'                    => false,
		'rest_base'                       => false,
		'rest_namespace'                  => false,
		'rest_controller_class'           => false,
		'autosave_rest_controller_class'  => false,
		'revisions_rest_controller_class' => false,
		'late_route_registration'         => false,
		'template'                        => array(),
		'template_lock'                   => false,
		'_builtin'                        => false,
		'_edit_link'                      => 'post.php?post=%d',
	);

	$args = array_merge( $defaults, $args );

	$args['name'] = $this->name;

	// If not set, default to the setting for 'public'.
	if ( null === $args['publicly_queryable'] ) {
		$args['publicly_queryable'] = $args['public'];
	}

	// If not set, default to the setting for 'public'.
	if ( null === $args['show_ui'] ) {
		$args['show_ui'] = $args['public'];
	}

	// If not set, default rest_namespace to wp/v2 if show_in_rest is true.
	if ( false === $args['rest_namespace'] && ! empty( $args['show_in_rest'] ) ) {
		$args['rest_namespace'] = 'wp/v2';
	}

	// If not set, default to the setting for 'show_ui'.
	if ( null === $args['show_in_menu'] || ! $args['show_ui'] ) {
		$args['show_in_menu'] = $args['show_ui'];
	}

	// If not set, default to the setting for 'show_in_menu'.
	if ( null === $args['show_in_admin_bar'] ) {
		$args['show_in_admin_bar'] = (bool) $args['show_in_menu'];
	}

	// If not set, default to the setting for 'public'.
	if ( null === $args['show_in_nav_menus'] ) {
		$args['show_in_nav_menus'] = $args['public'];
	}

	// If not set, default to true if not public, false if public.
	if ( null === $args['exclude_from_search'] ) {
		$args['exclude_from_search'] = ! $args['public'];
	}

	// Back compat with quirky handling in version 3.0. #14122.
	if ( empty( $args['capabilities'] )
		&& null === $args['map_meta_cap'] && in_array( $args['capability_type'], array( 'post', 'page' ), true )
	) {
		$args['map_meta_cap'] = true;
	}

	// If not set, default to false.
	if ( null === $args['map_meta_cap'] ) {
		$args['map_meta_cap'] = false;
	}

	// If there's no specified edit link and no UI, remove the edit link.
	if ( ! $args['show_ui'] && ! $has_edit_link ) {
		$args['_edit_link'] = '';
	}

	$this->cap = get_post_type_capabilities( (object) $args );
	unset( $args['capabilities'] );

	if ( is_array( $args['capability_type'] ) ) {
		$args['capability_type'] = $args['capability_type'][0];
	}

	if ( false !== $args['query_var'] ) {
		if ( true === $args['query_var'] ) {
			$args['query_var'] = $this->name;
		} else {
			$args['query_var'] = sanitize_title_with_dashes( $args['query_var'] );
		}
	}

	if ( false !== $args['rewrite'] && ( is_admin() || get_option( 'permalink_structure' ) ) ) {
		if ( ! is_array( $args['rewrite'] ) ) {
			$args['rewrite'] = array();
		}
		if ( empty( $args['rewrite']['slug'] ) ) {
			$args['rewrite']['slug'] = $this->name;
		}
		if ( ! isset( $args['rewrite']['with_front'] ) ) {
			$args['rewrite']['with_front'] = true;
		}
		if ( ! isset( $args['rewrite']['pages'] ) ) {
			$args['rewrite']['pages'] = true;
		}
		if ( ! isset( $args['rewrite']['feeds'] ) || ! $args['has_archive'] ) {
			$args['rewrite']['feeds'] = (bool) $args['has_archive'];
		}
		if ( ! isset( $args['rewrite']['ep_mask'] ) ) {
			if ( isset( $args['permalink_epmask'] ) ) {
				$args['rewrite']['ep_mask'] = $args['permalink_epmask'];
			} else {
				$args['rewrite']['ep_mask'] = EP_PERMALINK;
			}
		}
	}

	foreach ( $args as $property_name => $property_value ) {
		$this->$property_name = $property_value;
	}

	$this->labels = get_post_type_labels( $this );
	$this->label  = $this->labels->name;
}