3

I reckon this one is quick and easy... I think I'm just missing something very obvious. My inquiry is nearly identical to this stackoverflow post.

For non-admin users, the default filter for posts is 'mine' (the full filter list being: mine, all, published, drafts, pending, trash). So, when they click 'posts' for example, they are given a list of their own articles by default. I want it so the 'all' filter is the default.

Using the previously mentioned post, I have got this to work for posts flawlessly with this code:

add_action( 'load-edit.php', function() 
{
global $typenow;

// Not our post type, bail out
if( 'post' !== $typenow )
    return;

// Administrator users don't need this, bail out
if( current_user_can('add_users') )
    return;

// Only the Mine tab fills this conditions, redirect
if( !isset( $_GET['post_status'] ) && !isset( $_GET['all_posts'] ) )
{
    wp_redirect( admin_url('edit.php?all_posts=1') );
    exit();
}   
});

Great! Wonderful!

But I also want to apply this to a custom post type I've created. The post type slug is 'community', the plural label is 'Blog Posts' and the singular is 'Blog Post'. This is the code I produced based on the above code:

add_action( 'load-edit-community.php', function() 
{
global $typenow;

// Not our post type, bail out
if( 'community' !== $typenow )
    return;

// Administrator users don't need this, bail out
if( current_user_can('add_users') )
    return;

// Only the Mine tab fills this conditions, redirect
if( !isset( $_GET['blog_post_status'] ) && !isset( $_GET['all_blog_posts'] ) )
{
    wp_redirect( admin_url('edit.php?post_type=community&all_posts=1') );
    exit();
}   
});

I tried a few combinations but still can't seem to get it work (luckily they didn't bring my site down). I tried to place this above and below the working code for the regular 'post' type, but it didn't matter.

Would appreciate any help with this! Thanks.

EDIT: got it working now. seems i was trying to jump on a false hook. here is the working code:

add_action( 'load-edit.php', function() 
{
global $typenow;

// Not our post type, bail out
if( 'community' !== $typenow )
    return;

// Administrator users don't need this, bail out
if( current_user_can('add_users') )
    return;

// Only the Mine tab fills this conditions, redirect
if( !isset( $_GET['post_status'] ) && !isset( $_GET['all_posts'] ) )
{
    wp_redirect( admin_url('edit.php?post_type=community&all_posts=1') );
    exit();
}   
});

thanks @birgire!

2
  • I'm not sure load-edit-community.php is a hook that exists.
    – birgire
    May 12, 2015 at 9:25
  • ahhh!!! i see!! i got it working now because of this. thank you!
    – zk87
    May 12, 2015 at 13:02

2 Answers 2

2

answer ^^

add_action( 'load-edit.php', function() 
{
global $typenow;

// Not our post type, bail out
if( 'community' !== $typenow )
    return;

// Administrator users don't need this, bail out
if( current_user_can('add_users') )
    return;

// Only the Mine tab fills this conditions, redirect
if( !isset( $_GET['post_status'] ) && !isset( $_GET['all_posts'] ) )
{
    wp_redirect( admin_url('edit.php?post_type=community&all_posts=1') );
    exit();
}   
});
1
  • 1
    Still the correct approach and works in 2021
    – wittich
    Sep 6, 2021 at 22:13
-1

I think that a better solution would be to use the pre_get_posts hook and unset the author parameter.

It could be something like this:

add_action('pre_get_posts',function($query){
    //Some check. E.g checking a specific user role
    if ( ! current_user_can( 'author' ) ) {
         return;
    }

    unset( $query->query['author'] );
    unset( $query->query_vars['author'] );
});
1
  • This didn't work for me.
    – Mort 1305
    May 28, 2020 at 6:36

Your Answer

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

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