Created
May 12, 2016 10:36
-
-
Save Panchev/ac25f45bfbd92f4229db0d155bde3875 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function ls_export_csv() { | |
| // CSV file name | |
| $csv_file_name = 'Locations_'.date('Y-m-d').'.csv'; | |
| $csv_header_array = array( "Name", "Street", "City", "State", "Zip Code", "Phone Number", "Website", "Email"); | |
| header("Content-type: application/csv"); # Declare the file type | |
| header("Content-Transfer-Encoding: binary"); | |
| header("Content-Disposition: attachment; filename=".$csv_file_name); # Export the generated CSV File | |
| header("Pragma: no-cache"); | |
| header("Expires: 0"); | |
| ob_start(); | |
| $f = fopen('php://output', 'w') or show_error("Can't open php://output"); | |
| $n = 0; | |
| // Query all location posts | |
| $location_posts = get_posts('post_type=location&posts_per_page=-1&orderby=title&order=ASC'); | |
| if ( $location_posts ) { | |
| // Write the header of the CSV file | |
| if (isset($csv_header_array)) { | |
| if ( !fputcsv($f, $csv_header_array, ';')) | |
| { | |
| echo "Can't write line $n: $line"; | |
| } | |
| } | |
| foreach ($location_posts as $ap) { | |
| // Get all locations data in a single array | |
| $location_data = array( | |
| get_the_title($ap->ID), | |
| get_meta('_location_street', $ap->ID), | |
| get_meta('_location_city', $ap->ID), | |
| get_meta('_location_state', $ap->ID), | |
| get_meta('_location_zip', $ap->ID), | |
| get_meta('_location_phone', $ap->ID), | |
| get_meta('_location_website_email', $ap->ID), | |
| get_meta('_location_email', $ap->ID), | |
| ); | |
| // Generate the CSV row and put it into the file | |
| $n++; | |
| if ( !fputcsv($f, $location_data, ';')) { | |
| echo "Can't write line $n: $value"; | |
| } | |
| } | |
| fclose($f) or show_error("Can't close php://output"); | |
| $csvStr = ob_get_clean(); | |
| echo $csvStr; | |
| } // end Locations posts check | |
| exit; | |
| } // end Exporter |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment