WP Admin default view mode for Custom Post Type

To change it globally to excerpt for every post type I can use:

function my_default_posts_list_mode( $default ) {
  return 'excerpt';
}
add_filter( 'default-posts-list-mode', 'my_default_posts_list_mode' );

But how can I achieve the same only for a Custom Post Type?

Explanation:

I’ve created a Custom Post Type. In WP Admin the default view mode is set to list (edit.php?post_type=my_post_type&mode=list). I want it to be excerpt, but only for my Custom Post Type not affecting other post types.

I can do this manually by adding &mode=excerpt to the URL like so: edit.php?post_type=my_post_type&mode=excerpt, however I want this to be done automatically.

1 Answer
1

To change the mode URL variable but in the load try this:

add_action( 'load-edit.php', 'my_default_posts_list_mode' );
function my_default_posts_list_mode() {

    $post_type = isset( $_GET['post_type'] ) ? $_GET['post_type'] : '';
    if ( $post_type && $post_type == 'my_post_type' && !isset( $_REQUEST['mode'] ) )
        $_REQUEST['mode'] = 'excerpt';
}

Got the “insipration” from here: Set Default Listing “View” in Admin

Leave a Comment