Skip to content

Instantly share code, notes, and snippets.

@MikeNGarrett
Last active October 20, 2016 15:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MikeNGarrett/15c57181f85e24380b7a47db07d40f82 to your computer and use it in GitHub Desktop.
Save MikeNGarrett/15c57181f85e24380b7a47db07d40f82 to your computer and use it in GitHub Desktop.
If you need to switch a WordPress custom post type from post capability to page capability, do this.
<?php
/* This doesn't work and here's why not.
*
* The missing piece here has to do with rewrite rules.
* The registration process creates new rewrite rules during the process.
* This ensures the custom post type is set up appropriately.
* Modifying the url structure (which we're doing here) to hierarchical requires a change to the rewrite rules as well.
*
* The following is the first change you need to make. The rewrite change is not included here.
*/
// CAUTION! This could really ruin things, if you're not careful or aware of the changes this makes.
// Change `your_post_type` in line 6 to be the post type you're targeting.
// You may want to change $supports on line 12 to accommodate the custom post type's original functionality.
function change_cpt_to_page( $post_type, $post_type_object ) {
if ( 'your_post_type' != $post_type )
return;
$post_type_object->hierarchical = true;
$post_type_object->capability_type = 'page';
$supports = array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'page-attributes' );
add_post_type_support( $post_type_object->name, $supports );
global $wp_post_types;
$wp_post_types[$post_type] = $post_type_object;
}
add_action( 'registered_post_type', 'change_cpt_to_page', 99, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment