How to change arguments in an already registered custom post type?

Custom post types are a great way to organise and structure your data in WordPress. Many WordPress plugins use custom post types to implement their features.

But what if you want to make changes to the arguments added to the custom post type? Maybe you want to hide the custom post type from the REST API if it is included in its creation. In this example we set hierarchical to true for post type "product". This is done in the "customize_products" function. Additionally, we make an add_action hook where we enable page-attributes so that we can see the meta-box page-attributes in wp-admin for the post type. This is done in the function "products_attributes".

/* Changes to product post CPT */
add_filter( 'register_post_type_args', 'customize_products', 10, 2 );
function customize_products( $args, $post_type ) {
	// Let's make sure that we're customizing the post type we really need
	if ( $post_type !== 'product' ) {return $args;}
	
	// Now, we have access to the $args variable
	$args['hierarchical'] = true;
	
	return $args;
}

/* Post type support changes */
add_action( 'admin_init', 'products_attributes');
function products_attributes(){add_post_type_support( 'product', 'page-attributes' );}