Get multiple custom type posts at once in edit.php?post_type request

I tried it out to get an idea of the problems you will run into. The following code allows you to specify multiple post types with the multi_post_type parameter:

add_action( 'pre_get_posts', 'wpse12970_pre_get_posts' );
function wpse12970_pre_get_posts( &$wp_query )
{
    if ( is_admin() && array_key_exists( 'multi_post_type', $_GET ) ) {
        $wp_query->set( 'post_type', $_GET['multi_post_type'] );
        add_filter( 'the_posts', 'wpse12970_the_posts', 10, 2 );
    }
}

function wpse12970_the_posts( $posts, &$wp_query )
{
    $wp_query->set( 'post_type', $GLOBALS['post_type'] );
    return $posts;
}

The first problem was that the global $post_type should be a single type, otherwise other functions break. So we “smuggle” the multiple post types in under another name, and remove them again after the query ran.

The counter at the top of the list and the custom columns are based on only one post type. If there are many results paging will probably break.

If you want to do this, you should create your own list class, a child of WP_List_Table, like WP_Posts_List_Table but then for multiple post types. Because edit.php loads this table by default and I see no way to intercept it, you should create your own replacement of edit.php in your plugin and use that. I think it is doable, and probably interesting, but a lot of work.

Leave a Comment