admin管理员组

文章数量:1122826

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!

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!

Share Improve this question edited May 23, 2017 at 12:40 CommunityBot 1 asked May 12, 2015 at 9:17 zk87zk87 1191 gold badge3 silver badges9 bronze badges 2
  • I'm not sure load-edit-community.php is a hook that exists. – birgire Commented May 12, 2015 at 9:25
  • ahhh!!! i see!! i got it working now because of this. thank you! – zk87 Commented May 12, 2015 at 13:02
Add a comment  | 

3 Answers 3

Reset to default 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();
}   
});

This accepted answer did not work for me since I'd get an error saying too many redirects after bulk trashing posts while viewing the screen. Instead of trying to redirect the URLs like in the accepted answer or modify the query itself, I opted to changing the URLs in the admin sidebar to point to the "All" filter by including all_posts=1 in the URL like this:

/**
 * Filters the parent file of an admin menu sub-menu item.
 *
 * @param string $parent_file The parent file.
 *
 * @return string The parent file.
 */
function au_parent_file($file)
{
    $post_type = 'community'; // or whatever post type here

    // Only do it for the specific post type menu
    // And if the current user does not have the permission
    if ($file === 'edit.php?post_type=' . $post_type && !current_user_can('promote_users')) {
        global $menu, $submenu; // Access these global variables
        
        $parent_file = $file . '&all_posts=1'; // Adjust the URL of the to include "all_posts=1" so it defaults to "All"
        $key = au_get_menu_key($menu, $file); // My helper function below to get the key in the menu array
        $menu[$key][2] = $parent_file; // Update the value to the new one

        // Since the URLs are how submenus are associated, fix that too
        $subkey = au_get_menu_key($submenu[$file], $file); // My helper function below to get the key in the submenu array
        $submenu[$file][$subkey][2] = $parent_file; // Update the link within the submenu

        // Save its info before removing
        $i = array_search($file, array_keys($submenu)); // Store its position
        $value = $submenu[$file]; // Store its value

        // Remove it from the array
        unset($submenu[$file]);

        // Add it back with the corrected key in its original spot
        $submenu = array_slice($submenu, 0, $i, true) +
            array($parent_file => $value) +
            array_slice($submenu, $i, count($submenu) - $i, true);

        return $parent_file; // Return the corrected URL so it displays when viewing
    }

    return $file; // Return the original file as default
}
add_filter('parent_file', 'au_parent_file');

And then here is the little helper function I made to get those keys in the $menu and $submenu objects.

/**
 * Get Menu Key
 *
 * @param array $menu The menu array.
 * @param string $file The file to search for.
 *
 * @return int|string The key or index of the item with the `$file` value of the `$menu` array.
 */
function au_get_menu_key($menu, $file)
{
    $menu_columns = array_column($menu, 2);
    $index = array_search($file, $menu_columns);
    return array_combine(array_keys($menu_columns), array_keys($menu))[$index];
}

With that, the links in the sidebar, for the top-level post type button and it's "All {post type plural}" submenu link output /wp-admin/edit.php?post_type=community&all_posts=1 which will default you to viewing the "All" filter instead of "Mine"

Additionally, the add_users permissions was replaced in WordPress version 4.4 with promote_users (source).

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'] );
});

本文标签: Make the 39all39 filter default instead of 39mine39 in a custom post type