目前我得到了这个:
function redirect_companies()
{
if ( current_user_can( 'ca_company' ) )
{
$screen = get_current_screen();
if ( $screen->post_type != 'unternehmen' && $screen->id != 'profile' )
{
global $current_user;
$current_users_posts = get_posts(
array(
'post_type' => 'unternehmen',
'author' => $current_user->ID
)
);
if ( count( $current_users_posts ) > 1 )
{
$redirect = admin_url( 'edit.php?post_type=unternehmen' );
}
else
{
$redirect = get_edit_post_link( $current_users_posts[0]->ID );
}
wp_redirect( $redirect, 301 );
}
}
}
add_action('current_screen', 'redirect_companies');
它应该做什么:具有角色“ca_company”的用户登录到 wordpress 后端并立即被重定向到“unternehmen”的自定义帖子类型帖子的概览屏幕,或者,如果该用户仅存在一篇帖子,则重定向到编辑屏幕的那一篇文章。
此外,如果用户尝试访问不是来自帖子类型“unternehmen”且不是用户配置文件编辑屏幕的任何页面,它应该执行此重定向例程。
当我已经以 auch 用户身份登录然后尝试访问例如仪表板时,我成功地测试了这一点。这行得通。
但是,如果我完全退出 WP 然后再次登录,wordpress 正在执行此操作:
http://i.stack.imgur.com/M60aJ.png
...然后我的浏览器告诉我,存在重定向错误。无限重定向循环。但为什么?为什么它甚至会进入我检查帖子类型“unternehmen”的那个“如果”。因为如果我登录,我首先会进入仪表板......
希望有人可以提供帮助:)