// Define the date range
$start_date = '2010-11-09';
$end_date = '2014-06-26';
$args = array(
'post_type' => 'your-post-type',
'posts_per_page' => 1200,
'post_status' => 'publish',
'date_query' => array(
'after' => $start_date,
'before' => $end_date,
'inclusive' => true,
),
);
$query = new WP_Query($args);
// Prepare CSV data
$csv_data = array();
$csv_data[] = array('ID','URL','Name', 'POST');
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$post_id = get_the_ID();
$post_title = get_the_title();
$post_url = get_permalink();
$status = get_post_status();
$csv_data[] = array($post_id, $post_url, $post_title, $status);
}
wp_reset_postdata();
// Generate CSV file
$upload_dir = wp_upload_dir();
$csv_file_path = $upload_dir['basedir'] . '/' . date('Y/m') . '/posts_export.csv';
$csv_file = fopen($csv_file_path, 'w');
foreach ($csv_data as $line) {
fputcsv($csv_file, $line);
}
fclose($csv_file);
echo 'CSV file has been generated successfully. File path: ' . $csv_file_path;
} else {
echo 'No posts found within the specified date range.';
}
}
add_action( 'init','export_posts_to_csv');