public
Last active

Starting point class for Managing Orphaned Post Types in WordPress

  • Download Gist
orphaned-post-types.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
<?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;
}
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.