Skip to content

Instantly share code, notes, and snippets.

@mikeschinkel
Created November 2, 2010 22:35
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 mikeschinkel/660437 to your computer and use it in GitHub Desktop.
Save mikeschinkel/660437 to your computer and use it in GitHub Desktop.
Starting point class for Managing Orphaned Post Types in WordPress
<?php
/*
orphaned-post-types.php
View the list at http://yoursite.com/wp-admin/edit.php?orphans=true
In response to: http://lists.automattic.com/pipermail/wp-hackers/2010-November/035682.html
Not yet complete.
*/
OrphanedPostTypes::on_load();
class OrphanedPostTypes {
static $initial_post_types;
static function on_load() {
add_action('init',array(__CLASS__,'init'));
add_action('admin_menu',array(__CLASS__,'admin_menu'));
add_action('posts_where',array(__CLASS__,'posts_where'),10,2);
add_action('manage_posts_columns',array(__CLASS__,'manage_posts_columns'));
add_action('manage_posts_custom_column', array(__CLASS__,'manage_posts_custom_column'),10,2);
}
static function admin_menu() {
add_menu_page('Orphans', 'Orphans', 'edit_posts', 'edit.php?orphans=true');
add_submenu_page('edit.php?orphans=true','Manage Orphans', 'Manage Orphans', 'edit_posts', 'edit.php?orphans=true');
}
static function is_orphans($query) {
global $pagenow;
$is_orphans = false;
if ( $pagenow=='edit.php')
if (is_null($query->query)) {
$is_orphans = !empty($_GET['orphans']) && $_GET['orphans']=='true';;
} else {
$is_orphans = !empty($query->query_vars['orphans']) && $query->query_vars['orphans']=='true';
}
return $is_orphans;
}
static function init() {
global $pagenow;
global $wp;
if ($pagenow=='edit.php') {
self::$initial_post_types = get_post_types();
$wp->add_query_var('orphans');
self::register_unregistered_post_types();
}
}
static function register_unregistered_post_types() {
global $wp_query;
if (self::is_orphans($wp_query)) {
global $wpdb;
$post_types = implode("','",get_post_types());
$sql = "SELECT post_type FROM {$wpdb->posts} WHERE post_type NOT IN ('{$post_types}') GROUP BY post_type";
$post_types = $wpdb->get_col($sql);
foreach($post_types as $post_type) {
register_post_type($post_type,array(
'label' => ucfirst($post_type),
));
}
}
}
static function posts_where($where,$query) {
if (self::is_orphans($query)) {
$post_types = implode("','",self::$initial_post_types);
global $wpdb;
$where = " AND
{$wpdb->posts}.post_type NOT IN ('{$post_types}')";
}
return $where;
}
static function manage_posts_columns( $posts_columns ) {
global $wp_the_query;
if ( self::is_orphans($wp_the_query)) {
$posts_columns['post_type'] = 'Post Type';
}
return $posts_columns;
}
static function manage_posts_custom_column( $column_id,$post_id ) {
global $wp_the_query;
if ( self::is_orphans($wp_the_query)) {
switch ($column_id) {
case 'post_type':
$post = get_post($post_id);
echo $post->post_type;
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment