Forum Replies Created

Viewing 1 replies (of 1 total)
  • I’m a little late to the thread, but for all those who run into the same problem, here’s how I solved it.

    I was experiencing the same memory errors when the Delete Users page tried to query all 289,000+ users at once, so I wrote the following filter to limit the query to Authors, or more specifically, users with the role of Contributor and above, since those are the only users on my site that will have posts attributed to them anyway.

    /**
     * Filters the query that grabs all users in the database for the Select field on the Delete Users page,
     * restricting the query to Contributors and above
     */
    function delete_user_select_only_authors( $query ) {
    	// make sure we are on the Delete Users page
    	if ('/wp-admin/users.php' == $_SERVER['PHP_SELF'] && isset($_GET['action']) && 'delete' == $_GET['action']) {
    		global $wpdb;
    		// grabbing table names for convenience, taking into account custom prefix
    		$users = $wpdb->users;
    		$usermeta = $wpdb->usermeta;
    		$id = $_GET['user'];
    		// only alter the query attempting to select all users in the database
    		if ($query == "SELECT $users.ID,$users.display_name FROM $users WHERE 1=1 AND $users.ID NOT IN ($id) ORDER BY display_name ASC ") {
    			// create a replacement WHERE clause to select only Contributors and above
    			$join_where = "JOIN $usermeta ON $users.ID = $usermeta.user_id WHERE $usermeta.meta_key = 'wp_user_level' AND $usermeta.meta_value > 0";
    			// replace the new WHERE in the original query
    			$query = str_replace('WHERE 1=1', $join_where, $query);
    		}
    	}
    	return $query;
    }
    add_filter( 'query', 'delete_user_select_only_authors' );
Viewing 1 replies (of 1 total)