I needed to export a list of posts from wordpress to check the category and posting status. I got code from ChatGPT and did some testing which works correctly.
I deactivated the plugin and run it as required as I dont want this available to any site visitors, although the code should not allow this!. Save the Code below to your plugin folder and activate it.
To export the post use this syntax https://yourwebsite.com/wp-admin/?export_posts_to_csv
Plugin to Export a list of posts from Wordpress
<?php
/*
Plugin Name: Export Posts to CSV
Description: Export posts with title, category, and post date to a CSV file.
Version: 1.0
Author: Anything Access
Author URI: https://AnythingAccess.com
*/
function export_posts_to_csv() {
if (isset($_GET['export_posts_to_csv'])) {
// Ensure the user is logged in and has administrator privileges
if (!is_user_logged_in() || !current_user_can('manage_options')) {
wp_die('You do not have sufficient permissions to access this page.'); // Exit with an error message
}
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="posts.csv"');
$output = fopen('php://output', 'w');
fputcsv($output, ['Title', 'Category', 'Post Date', 'Status', 'URL']);
// Query to get posts of all statuses (published, draft, scheduled, etc.)
$args = [
'post_type' => 'post',
'post_status' => ['publish', 'draft', 'future'], // Include published, drafts, and scheduled posts
'posts_per_page' => -1,
];
$posts = get_posts($args);
foreach ($posts as $post) {
$categories = wp_get_post_categories($post->ID, ['fields' => 'names']);
$post_status = get_post_status($post->ID); // Get the post status
$url = $post->post_name;
fputcsv($output, [$post->post_title, implode(', ', $categories), $post->post_date, $post_status, $url]);
}
fclose($output);
exit;
}
}
add_action('admin_init', 'export_posts_to_csv');
Click here to see all post related to Wordpress or Chatgpt
Version 1.1 9th Jan – Updated the code today to add pages and posts with the keyword and snippet.
You can choose from either of these functions , but as the function name is the same only save one in your plugin folder.
<?php
/*
Plugin Name: Export Posts to CSV
Description: Export posts with title, category, and post date to a CSV file.
Version: 1.1
Author: Anything Access
Author URI: https://AnythingAccess.com
*/
function export_posts_to_csv() {
if (isset($_GET['export_posts_to_csv'])) {
// Ensure the user is logged in and has administrator privileges
if (!is_user_logged_in() || !current_user_can('manage_options')) {
wp_die('You do not have sufficient permissions to access this page.'); // Exit with an error message
}
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="posts.csv"');
$output = fopen('php://output', 'w');
fputcsv($output, ['Type','Title', 'Category', 'Post Date', 'Status', 'Keyword', 'Snippet', 'URL']);
// Query to get posts of all statuses (published, draft, scheduled, etc.)
$args = [
'post_type' => ['post','page'],
'post_status' => ['publish', 'draft', 'future'], // Include published, drafts, and scheduled posts
'posts_per_page' => -1,
];
$posts = get_posts($args);
foreach ($posts as $post) {
$categories = wp_get_post_categories($post->ID, ['fields' => 'names']);
$post_status = get_post_status($post->ID); // Get the post status
$post_snippet = get_the_excerpt($post->ID);
$focus_keyword = get_post_meta($post->ID, 'rank_math_focus_keyword', true) ?: 'N/A';
$url = $post->post_name;
fputcsv($output, [$post->post_type,$post->post_title, implode(', ', $categories), $post->post_date, $post_status,$focus_keyword,$post_snippet, $url]);
}
fclose($output);
exit;
}
}
add_action('admin_init', 'export_posts_to_csv');
Click here to open ChatGPT