Output Users of a Specific Role in the Yoast Sitemap : WordPress Code Snippet

The following code will only return authors of a specific role from the generated Yoast XML sitemap. In the example, users of the ‘editor’ role are returned.

Add the following to your functions.php file:

/**
 * Filter Yoast authors - exclude all authors except those with a specific role
 */

add_filter( 'wpseo_sitemap_exclude_author', function( $users ) {
    $required_user_role = 'editor';
    $required_users = [];

    foreach ( $users as $user ) {
        if ( true === in_array( $required_user_role, $user->roles ) ) {
            $required_users[] = $user;
        }
    }

    return $required_users;
}, 10, 1 );