尝试从数据库表行创建Wordpress中的帖子

I'm attempting to write a plugin to automatically create posts based on rows in a new database table, however I'm having issues as it's looping, and if I put an echo, it would echo out code, but it's not creating the new posts. I'm not getting any error messages either. What am I missing?

EDIT: I have modified my code

global $wpdb;
$rows = $wpdb->get_results("SELECT * FROM routes_txt");
foreach ( $rows as $row ) {

    // Insert the post into the database
    $my_post = array(
      'post_title'    => $row->route_short_name,
      'post_content'  => $row->route_id,
      'post_status'   => 'publish',
      'post_type'     => 'routes',
      'post_author'   => get_current_user_id(),
    );

    wp_insert_post( $my_post );
};

But now it's only doing the first row, and returning an error:

Fatal error: Uncaught Error: Call to undefined function is_user_logged_in() in /app/public/wp-includes/post.php:2283

Got it fixed, thanks! wrapped it in a function and executed it, and that fixed all the issues.