Skip to content

Instantly share code, notes, and snippets.

@rodriigomedeiros
Last active May 10, 2017 18:09
Show Gist options
  • Save rodriigomedeiros/783895ea19e51bf32d31078632ad8cd0 to your computer and use it in GitHub Desktop.
Save rodriigomedeiros/783895ea19e51bf32d31078632ad8cd0 to your computer and use it in GitHub Desktop.
Exportar posts wordpress para XLSX
<?php
if ( is_admin() ) {
function admin_footer_guiaprofissionais() {
global $post_type;
if ( $post_type == 'profissional' ) {
?>
<script type="text/javascript">
jQuery(document).ready(function () {
jQuery('<option>').val('export').text('<?php echo _e( 'Export' ); ?>').appendTo("select[name='action']");
});
</script>
<?php
}
}
function export_guia_profissionais() {
$wp_list_table = _get_list_table( 'WP_Posts_List_Table' );
$action = $wp_list_table->current_action();
$report = array();
if ( isset( $_REQUEST['post'] ) ) {
$post_ids = array_map( 'intval', $_REQUEST['post'] );
}
if ( $action === 'export' ) {
$args = [
'post_type' => 'profissional',
'meta_key' => 'mediavaliacao-geral',
'post__in' => $post_ids,
'orderby' => 'meta_value',
'order' => 'DESC',
'meta_query' => [
[
'key' => 'mediavaliacao-geral'
],
]
];
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) {
$the_query->the_post();
$terms = get_the_terms( get_the_ID(), 'especialidade' );
$entryTerms = "";
if ( ! empty( $terms ) ) {
foreach ( $terms as $term ) {
$entryTerms .= $term->name . ', ';
}
}
$entryTerms = rtrim( $entryTerms, ', ' );
$telefones = get_field( 'contato-telefones', get_the_ID() );
$entryTelefones = "";
if ( ! empty( $telefones ) ) {
foreach ( $telefones as $telefone ) {
if ( $telefone['numero'] != "" ) {
$entryTelefones .= $telefone['numero'] . ' - ' . $telefone['operadora'] . ', ';
}
}
$entryTelefones = rtrim( $entryTelefones, ', ' );
}
$report[] = [
get_the_title(),
get_the_content(),
get_field( 'cursosrealizados', get_the_ID() ) ?: '',
$entryTerms,
( get_field( 'contato-endereco-bairro', get_the_ID() ) . ', ' . get_field( 'contato-endereco-cidade', get_the_ID() ) . ' - ' . get_field( 'contato-endereco-uf', get_the_ID() ) ) ?: '',
get_field( 'contato-email', get_the_ID() ) ?: '',
get_field( 'contato-whatsapp', get_the_ID() ) ?: '',
$entryTelefones,
get_field( 'mediavaliacao-geral', get_the_ID() ) ?: 'Nenhuma avaliação publicada',
];
}
wp_reset_postdata();
$objPHPExcel = new PHPExcel();
$rowNumber = 1;
$headings = [
'Nome',
'Descrição',
'Cursos Realizados',
'Especialidade',
'Bairro/Cidade - UF',
'E-mail',
'WhatsApp',
'Telefones para Contato',
'Avaliação Geral',
];
$objPHPExcel->getActiveSheet()->fromArray( array( $headings ), null, 'A' . $rowNumber );
$rowNumber ++;
foreach ( $report as $row ) {
$col = 'A';
foreach ( $row as $cell ) {
$objPHPExcel->getActiveSheet()->setCellValue( $col . $rowNumber, $cell );
$col ++;
}
$rowNumber ++;
}
$filename = sanitize_file_name( sprintf( 'file-name-%s', date( 'Y-m-d-U' ) ) );
header( "Pragma: public" );
header( "Expires: 0" );
header( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
header( "Content-Type: application/force-download" );
header( "Content-Type: application/octet-stream" );
header( "Content-Type: application/download" );
header( "Content-Disposition: attachment; filename={$filename}.xlsx" );
header( "Content-Transfer-Encoding: binary " );
header( 'Content-Description: Description', true );
$objWriter = new PHPExcel_Writer_Excel2007( $objPHPExcel );
$objWriter->setOffice2003Compatibility( true );
$objWriter->save( 'php://output' );
exit;
} else {
return;
}
exit;
}
add_action( 'admin_footer', 'admin_footer_guiaprofissionais' );
add_action( 'load-edit.php', 'export_guia_profissionais' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment