Advertisement
Guest User

Untitled

a guest
Dec 9th, 2018
536
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.83 KB | None | 0 0
  1. add_action( 'restrict_manage_posts', 'add_export_button' );
  2. function add_export_button() {
  3.     $screen = get_current_screen();
  4.  
  5.     if (isset($screen->parent_file) && ('edit.php?post_type=certificate' == $screen->parent_file)) {
  6.         ?>
  7.         <input type="submit" name="export_all_posts" id="export_all_posts" class="button button-primary" value="Export All Posts">
  8.         <script type="text/javascript">
  9.             jQuery(function($) {
  10.                 $('#export_all_posts').insertAfter('#post-query-submit');
  11.             });
  12.         </script>
  13.         <?php
  14.     }
  15. }
  16.  
  17. add_action( 'init', 'func_export_all_posts' );
  18. function func_export_all_posts() {
  19.     if(isset($_GET['export_all_posts'])) {
  20.         if(isset($_GET['list'])) {
  21.             $filter = strval($_GET['list']);
  22.         };
  23.         $arg = array(
  24.                 'post_type' => 'certificate',
  25.                 'post_status' => 'publish',
  26.                 'posts_per_page' => -1,
  27.                 'tax_query' => array(
  28.                     array(
  29.                         'taxonomy' => 'list',
  30.                         'field'    => 'slug',
  31.                         'terms'    => $filter ,
  32.                     )
  33.                     ),
  34.             );
  35.  
  36.         global $post;
  37.         $arr_post = get_posts($arg);
  38.         if ($arr_post) {
  39.  
  40.             header('Content-type: text/csv');
  41.             header('Content-Disposition: attachment; filename="wp.csv"');
  42.             header('Pragma: no-cache');
  43.             header('Expires: 0');
  44.  
  45.             $file = fopen('php://output', 'w');
  46.  
  47.             fputcsv($file, array('Post Title', 'URL'));
  48.  
  49.             foreach ($arr_post as $post) {
  50.                 setup_postdata($post);
  51.                 fputcsv($file, array(get_the_title(), get_the_permalink()));
  52.             }
  53.  
  54.             exit();
  55.         }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement