I am building a site with 4 custom post types.
To make things easier for our client/admin, I’m wondering if there is a way to merge those custom post types into one list in the backend? Like if you look at normal backend admin page with the list and columns of posts, can a page like that pull in 3 other post types for easy sorting/searching?

I can’t combine the CPTs into one, they must remain seperate outside of one ‘viewing’ page in the admin area if possible.

Any thoughts on how to do this?? Or even where to start looking/thinkging of how to develop this?

3 s
3

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 Reply

Your email address will not be published. Required fields are marked *