So, I’m back.
I had to change your code suggestion a little bit since your lines
if( in_array( 'author', (array) $user->roles ) ) {
return;
}
stop the script if the persons role is “author” (or whatever I choose). Every other user would have been deleted.
But I want the script to check the other way around: the script should stop if the users role isn’t “subscriber” – and this way only delete subscribers:
function auto_delete_user_without_ads( $user_id ) {
$user = get_user_by( "ID", $user_id );
if( ! $user ) {
return;
}
if( !in_array( 'subscriber', (array) $user->roles ) ) { // if role is not "subscriber" --> stop
return;
}
$query = new WP_Query([
"post_type" => "any",
"posts_per_page" => 1,
"author" => $user_id,
"fields" => "ids"
]);
if( $query->post_count === 0 ) {
require_once( ABSPATH.'wp-admin/includes/user.php' );
wp_delete_user( $user_id );
}
}
During my testing I saw a new point that I didn’t see before:
There are still hundreds of user accounts without adverts (they have been expired and then deleted automatically or they have been deleted manually) in my database that I also would like to have deleted automatically.
How is it possible to execute the code above not after a post got deleted automatically but reguarly, on an every day base? This would clean up the database since every subscriber-user-account without ads (no active and no expired ones) would be deleted.
Thank you!
Titus