I have the following code to produce a CSV file from content in Wordpress but I need to add headers to the four columns that are produced. Where can I define these as they will always be fixed names?
$fileName = 'Optical_test_results.csv';
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Description: File Transfer');
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename={$fileName}");
header("Expires: 0");
header("Pragma: public");
$fh = @fopen( 'php://output', 'w' );
$arr_query_args = array(
'numberposts' => -1,
'orderby' => 'post_title',
'post_type' => 'page',
'exclude' => '24'
);
$arr_posts = get_posts( $arr_query_args );
global $post;
foreach( $arr_posts as $this_post ) {
$permalink = get_permalink($this_post->ID);
$page_number = get_post_meta($this_post->ID, "part_number", true);
$serial = $this_post->post_title;
$s1 = get_post_meta($this_post->ID, 'tracking_number', true);
$s2 = get_the_title($page_number);
$s3 = $permalink;
$results = $serial.','.$s1.','.$s2.','.$s3."
";
echo $results;
fputcsv($fh);
}
$headerDisplayed = false;
fclose($fh);
exit;