Skip to content

Instantly share code, notes, and snippets.

@samuelguebo
Created October 26, 2018 21:37
Show Gist options
  • Save samuelguebo/26e9301a09c953880f2a27445733e867 to your computer and use it in GitHub Desktop.
Save samuelguebo/26e9301a09c953880f2a27445733e867 to your computer and use it in GitHub Desktop.
WordPress posts to CSV using WPDB class
<?php
# Author: Samuel Guebo
# reference: https://codex.wordpress.org/Class_Reference/wpdb
# Description: A simple snippet to display WordPress posts as in CSV (comma separated format).
# The script uses WPDB class and can accessed directly if put in the Website's root (/script.php)
// Include WordPress core features
require_once("wp-blog-header.php");
// Leverage WPDB class, utility for perfoming DB actions
global $wpdb;
// Build the query and get all posts with type "page"
$result = $wpdb->get_results ( "
SELECT *
FROM $wpdb->posts
WHERE post_type = 'page'
" );
// Print headers of CSV columns
echo 'ID, TITLE, LINK' . '<br/>';
// Print rows of the CSV document
foreach ( $result as $page )
{
echo $page->ID . ", " . $page->post_title.', '. get_the_permalink($page->ID) . '<br/>';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment