Merge two custom post types into one admin page?

Just a starting point, as surely issues will popup during further development. For example, right now, the search functionality breaks as it expects a string (post_type) and it’s receiving an array.

To list more than one post type in the Posts screen, we hook into pre_get_posts and modify the query. In this test, Posts, Pages and Products will be shown together in the Posts screen (http://example.com/wp-admin/edit.php).

add_action( 'pre_get_posts', 'join_cpt_list_wspe_113808' );

function join_cpt_list_wspe_113808( $query ) 
{
    // If not backend, bail out
    if( !is_admin() )
        return $query;

    // Detect current page and list of CPTs to be shown in Dashboard > Posts > Edit screen
    global $pagenow;
    $cpts = array( 'post', 'page', 'product' );

    if( 'edit.php' == $pagenow && ( get_query_var('post_type') && 'post' == get_query_var('post_type') ) )
        $query->set( 'post_type', $cpts );

    return $query;
}

A helper code to show a column with each post Post Type:

add_filter( 'manage_edit-post_columns', 'add_cpt_column_wspe_113808' );
foreach( array( 'post', 'page', 'product' ) as $cpt )
    add_action( "manage_{$cpt}_posts_custom_column", 'show_cpt_column_wspe_113808', 10, 2 );

function add_cpt_column_wspe_113808( $columns ) 
{
    $columns[ 'cpt' ] = 'Post Type';
    return $columns;
}

function show_cpt_column_wspe_113808( $column_name, $post_id ) 
{
    if ( 'cpt' != $column_name )
        return;
    echo get_post_type( $post_id );
}

Leave a Comment

error code: 523