Birchpress – How Do I Export Client Appointments?

As of 2015, from what i’ve read on forums and the Birchpress documentation, you can’t — as of a few minutes ago, i have at least something working!

This is just a PHP script that you can put in a hook or whatever – but in my scenario I’m just going to the URL example.com/services/exportAppointments.php

First we want to include wordpress functionality in our service: (mine’s in a folder i made called “services”, so i need to go up a directory to the be in the root, thus the ‘../’)

<?php 
include '../wp-load.php'; 
?>

We’ll need two functions, one to find an appointment and one to find a client by an ID:

<?php
/***********************************
  Query Single Appointment Details
***********************************/ 
function getAppointment($postID){
  query_posts(array( 
    'post_type' => 'birs_appointment',
    'p' => $postID
  ));  
  while (have_posts()) : the_post(); 
    $data = get_post_meta(get_the_ID()); 
  endwhile;
  wp_reset_query();
  return $data;
}
?>
<?php
/***********************************
  Query Single Client Details
***********************************/ 
function getClient($postID){
  query_posts(array( 
    'post_type' => 'birs_client',
    'p' => $postID
  ));  
  while (have_posts()) : the_post(); 
    $data = get_post_meta(get_the_ID()); 
  endwhile;
  wp_reset_query();
  return $data;
}
?>

The post type that links these together is called “birs_appointment1on1”, so getting a query of those (you can apply your own filters here) will give me the list of all the appointments, which from there I can get the relevant info of the various clients and appointments. There may be a more sleek way to do this so you don’t re-query those, but this is just a quickie. (please note, i am using the client Zipcode as a zipcode for their private practice, and i create a custom field for their practice name, so there’s an example of custom data in there too!)

<?php
/*****************************************
  Query the Various Appointments
*****************************************/ 
$birchpress_appointments = query_posts(array( 
  'post_type' => 'birs_appointment1on1', 
  'showposts' => -1
)); 
foreach($birchpress_appointments as $birchpress_appointment): 
  $birchID = $birchpress_appointment->ID;
  $data = get_post_meta($birchID); 

  //CLIENT
  $clientID = $data['_birs_client_id'][0];
  $client = getClient($clientID);
  //APPOINTMENT
  $appointmentID = $data['_birs_appointment_id'][0];
  $appointment = getAppointment($appointmentID);
endforeach;
?>

So, let’s put it all together and export the data as a csv – hopefully this provides a good starting point for anyone that wants to take the last query and apply meta filters to the _birs_appointment_id and _birs_client_id fields

<?php 
include '../wp-load.php'; 

//OUTPUT HEADERS - makes browser download a CSV
header('Content-type: text/csv');
header('Content-Disposition: attachment; filename="appointments.csv"');
header('Pragma: no-cache');
header('Expires: 0');
$file = fopen('php://output', 'w');

// COLUMN HEADERS
fputcsv($file, array(
  'First Name', 
  'Last Name', 
  'Email', 
  'Phone', 
  'Practice Name', 
  'Practice Zip', 
  "Appointment"
));
$csvdata = array();

/*****************************************
  Query the Various Appointments
*****************************************/ 
$birchpress_appointments = query_posts(array( 
  'post_type' => 'birs_appointment1on1', //birs_appointment
  'showposts' => -1
)); 
foreach($birchpress_appointments as $birchpress_appointment): 
  $birchID = $birchpress_appointment->ID;
  $data = get_post_meta($birchID); 

  //CLIENT DATA
  $clientID = $data['_birs_client_id'][0];
  $client = getClient($clientID);
  $fname = $client['_birs_client_name_first'][0];
  $lname = $client['_birs_client_name_last'][0];
  $email = $client['_birs_client_email'][0];
  $phone = $client['_birs_client_phone'][0];
  $practiceName = $client['_birs_field_6'][0];
  $practiceZip = $client['_birs_client_zip'][0];

  //APPOINTMENT DATA
  $appointmentID = $data['_birs_appointment_id'][0];
  $appointment = getAppointment($appointmentID);
  $timestamp = $appointment['_birs_appointment_timestamp'][0];
  
  // COLUMN ROWS -- match COLUMN HEADERS array structure
  $csvdata[] = array(
    $fname,
    $lname,
    $email,
    $phone,
    $practiceName,
    $practiceZip,
    date('Y-m-d H:i:s', $timestamp)
  );

endforeach;
wp_reset_query();

// output each row of the data
foreach ($csvdata as $row){
  fputcsv($file, $row);
}
exit();


/***********************************
  Query Single Appointment Details
***********************************/ 
function getAppointment($postID){
  query_posts(array( 
    'post_type' => 'birs_appointment',
    'p' => $postID
  ));  
  while (have_posts()) : the_post(); 
    $data = get_post_meta(get_the_ID()); 
  endwhile;
  wp_reset_query();
  return $data;
}


/***********************************
  Query Single Client Details
***********************************/ 
function getClient($postID){
  query_posts(array( 
    'post_type' => 'birs_client',
    'p' => $postID
  ));  
  while (have_posts()) : the_post(); 
    $data = get_post_meta(get_the_ID()); 
  endwhile;
  wp_reset_query();
  return $data;
}

?>