Skip to content

Instantly share code, notes, and snippets.

@richjenks
Last active December 15, 2016 16:54
Show Gist options
  • Save richjenks/9d2a91ee7940670c4c5db657da9e3e57 to your computer and use it in GitHub Desktop.
Save richjenks/9d2a91ee7940670c4c5db657da9e3e57 to your computer and use it in GitHub Desktop.
Moves all pages of a CPT (Custom Post Type) into one menu item
<?php
/**
* Moves all pages of a CPT (Custom Post Type) into one menu item
* Useful when a CPT is a submenu & the Add New page goes walkies
*
* Example: You have added a new CPT under an existing CPT or
* menu item. When you click "Add New" for the new CPT, the URL
* doesn't match a menu item so nothing is selected as the
* current page. This function makes such pages show as the main
* CPT page in the menu when selected.
*
* @param string|array $post_types Post Types to compact
*/
function compact_post_types( $post_types ) {
if ( !is_array( $post_types ) ) $post_types = [ $post_types ];
add_filter( 'parent_file', function( $parent_file ) use ( $post_types ) {
foreach ( $post_types as $post_type ) {
if ( self::get_post_type() == $post_type ) {
$GLOBALS['submenu_file'] = 'edit.php?post_type=' . $post_type;
return $parent_file;
}
}
} );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment