Using built-in WP functions to get the data is usually a better approach. It can be more performant, and is preferable security-wise because you’re using prebuilt queries instead of building your own.
In a plugin, you can use something like:
<?php
// Get all published CPTs
$allEvents = get_posts(array(
'post_type' => 'ajde_events',
'post_status' => 'publish',
'numberposts' => -1
));
// If any were found
if($allEvents) {
// Open/create your csv (your code here)
// Loop through all CPTs and grab postmeta
foreach($allEvents as $event) {
$meta = get_post_meta($event);
// Save the postmeta with the post object
$event->evcal_location = $meta['evcal_location'];
$event->evcal_srow = $meta['evcal_srow'];
// Add the full post object, with postmeta, to an array
$fullData[] = $event;
}
// Reset the query
wp_reset_postdata();
// Save to the csv and close it (your code here)
}
?>
Where to put this code depends on how you want the plugin to work. Perhaps you want to create an admin screen where a user can manually press a button to download the CSV, or perhaps you want a cron job that automatically builds it every day and emails the file.