Set Default Listing “View” in Admin

The question:

I’m looking for a way to set the default “View” options under post types in the admin area. For example, by default Posts will default to a “list” view instead of “excerpt” – I’d like to change this to excerpt by default – what would be the best way to achieve this? I’ve done a bit of searching on this, but am stuck in the mud thus-far. Any help would be greatly appreciated. Thanks!

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

Although having the feature of persistent settings in core is nice, it may take quite a while before it’s actually accepted. WordPress 3.5 is still quite far away.

So let’s augment the global $_REQUEST array instead.

add_action( 'load-edit.php', 'wpse34956_force_excerpt' );
function wpse34956_force_excerpt() {
    $_REQUEST['mode'] = 'excerpt';
}

This will lock up modes, forcing excerpt mode all the time, so let’s turn let the user decide but keep it persistent using the user’s metadata:

add_action( 'load-edit.php', 'wpse34956_persistent_posts_list_mode' );
function wpse34956_persistent_posts_list_mode() {
    if ( isset( $_REQUEST['mode'] ) ) {
        // save the list mode
        update_user_meta( get_current_user_id(), 'posts_list_mode', $_REQUEST['mode'] );
        return;
    }
    // retrieve the list mode
    if ( $mode = get_user_meta( get_current_user_id(), 'posts_list_mode', true ) )
        $_REQUEST['mode'] = $mode;
}

You can further interpolate post_type into all by taking into account the $_GET['post_type'] variable when available.

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

    // take into account post types that support excerpts
    $post_type = isset( $_GET['post_type'] ) ? $_GET['post_type'] : '';
    if ( $post_type && !post_type_supports( $post_type, 'excerpt' ) )
        return; // don't care

    if ( isset( $_REQUEST['mode'] ) ) {
        // save the list mode
        update_user_meta( get_current_user_id(), 'posts_list_mode' . $post_type, $_REQUEST['mode'] );
        return;
    }

    // retrieve the list mode
    if ( $mode = get_user_meta( get_current_user_id(), 'posts_list_mode' . $post_type, true ) )
        $_REQUEST['mode'] = $mode;
}

Viola! Persistent list mode per post type per user, no hacks.

Method 2

The post view screen switches from list view to excerpt view based on the value of the “mode” parameter in the query string. If the “mode” parameter is not set, then WordPress defaults to list view.

Unfortunately, this parameter is not filterable, so there’s no easy way to control it programatically.

So I’m going to do something I never do … I’m going to tell you how to hack Core to make this work …

Adding a filter

Open /wp-admin/includes/class-wp-posts-list-table.php and find the prepare_items() method (around line 81).

On line 99, WordPress checks to see whether or not the “mode” parameter was set in the request and uses this to set the global $mode variable:

$mode = empty( $_REQUEST['mode'] ) ? 'list' : $_REQUEST['mode'];

We’re going to change this line to filter the default setting. Change this line to:

$mode = empty( $_REQUEST['mode'] ) ? apply_filters( 'default-posts-list-mode', 'list' ) : $_REQUEST['mode'];

Now, go into your theme’s functions.php file and add the following code:

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

This will hook in to the filter and return excerpt mode by default.

Since my personal rule about hacking Core requires that all hacks be contributed back to the project (this way they can be rolled into Core and no longer count as a hack), I’ve opened a Trac ticket for this enhancement and submitted the code above as a patch. Please weigh in on the ticket so that it can get into core for 3.5 (we’re too late in the cycle for 3.4, but we can try to push this through for the next version).

Method 3

Okay, so shortly after I put a bounty up, I came up with the following solution. It is the default behaviour in every way, except that it selects excerpt view for whatever post type you want (instead of the default list view).

<?php

add_action( 'admin_init', 'my_admin_init' ); // initiate admin hook

function my_admin_init() {
    // if mode is not set redirect to a default mode.
    if(!isset($_GET['mode'])) {
        if('forms' == $_GET['post_type'] || 'client-quotes' == $_GET['post_type'] ) {
            wp_redirect( admin_url( 'edit.php?mode=excerpt&' . http_build_query( $_GET ) ) );
            exit;
        }
    }
}

?>

NOTE: I recommend Soulseekah’s approach, if you don’t want it to remember the user’s choice, you can incorporate my code with his code a bit.
NOTE 2: If/when EAMann’s patch is part of the core, obviously his method would be the best one as it wouldn’t require you to go the long way round. I just don’t like it at the moment as you have to edit the core files.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment