小编Mik*_*son的帖子

WordPress 过滤器用于更改编辑帖子作者框中的作者列表

我想更改自定义帖子类型的编辑帖子页面上“作者选择”下拉列表中的用户列表。我可以使用过滤器挂钩吗?我无法找到任何有关过滤器挂钩的信息来满足我的要求。

该钩子(理论上)应该让我返回一个用户数组,这些用户将填充底部的选择框。我想这样做的原因是我可以根据不同帖子类型的角色有条件地过滤掉用户。作为管理员(或其他管理员),我不想在将用户设为作者之前检查用户是否具有特定角色。

代码示例:

add_filter('example_filter', 'my_custom_function');
function my_custom_function ( $users ){

    // Get users with role 'my_role' for post type 'my_post_type'
    if( 'my_post_type' == get_post_type() ){
        $users = get_users( ['role' => 'my_role'] );
    }

    // Get users with role 'other_role' for post type 'other_post_type'
    if( 'other_post_type' == get_post_type() ){
        $users = get_users( ['role' => 'other_role'] );
    }

    return $users;
}
Run Code Online (Sandbox Code Playgroud)

php wordpress custom-post-type wordpress-hook

6
推荐指数
1
解决办法
3131
查看次数

标签 统计

custom-post-type ×1

php ×1

wordpress ×1

wordpress-hook ×1