3

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.

2
  • Can you expand more on the problem you are having? Thanks. Commented Nov 7, 2014 at 17:27
  • @iambriansreed I've updated my question.
    – SLH
    Commented Nov 7, 2014 at 17:34

1 Answer 1

7
+25

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

11
  • Thanks, but where I can set my CPT name?
    – SLH
    Commented Nov 7, 2014 at 17:46
  • Sorry, just added it now. Check it out. Commented Nov 7, 2014 at 17:50
  • There are two buttons under "Search" in posts listing: List View and Excerpt View. I can't switch back to List View. And your code affects all post types ;/
    – SLH
    Commented Nov 7, 2014 at 17:58
  • Ok. Should be good now. Change my_post_type to the necessary post type. Commented Nov 7, 2014 at 17:58
  • I've change my_post_type in your updated code but it's still affecting all post types and I can't switch back to list view in my custom post type. It's weird - I'm dealing with it about 3 days ;s
    – SLH
    Commented Nov 7, 2014 at 18:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.