Up until now, my client has been using WordPress posts as his way to display his products. I now need to export all these posts into a CSV format so that I can format the CSV file and import it back into WP e-Commerce.
I've looked at number of options including plugins to export the posts, exporting directly from phpMyAdmin and using the XML export option within WP but none is coming out as clean as I would like it.
I think I basically need an SQL query that I can run to export only the information from the POSTS table, excluding any other rubbish such as ID numbers, ping status, guid etc.
All I need is:
- Post Title
- Post Content
- Custom Fields
- Categories
Here's the start of my Query
SELECT *
FROM `wp_posts`
WHERE `post_status` = 'publish'
EDIT: I am open to better solutions if anyone thinks there is a better way to do this?
EDIT
Here's a solution I found on SO but I am not sure where to use this?
<?php
function array2xml($array, $name='array', $standalone=TRUE, $beginning=TRUE)
{
global $nested;
if ($beginning)
{
if ($standalone) header("content-type:text/xml;charset=utf-8");
$output .= '<'.'?'.'xml version="1.0" encoding="UTF-8"'.'?'.'>' . PHP_EOL;
$output .= '<' . $name . '>' . PHP_EOL;
$nested = 0;
}
// This is required because XML standards do not allow a tag to start with a number or symbol, you can change this value to whatever you like:
$ArrayNumberPrefix = 'ARRAY_NUMBER_';
foreach ($array as $root=>$child)
{
if (is_array($child))
{
$output .= str_repeat(" ", (2 * $nested)) . ' <' . (is_string($root) ? $root : $ArrayNumberPrefix . $root) . '>' . PHP_EOL;
$nested++;
$output .= array2xml($child,NULL,NULL,FALSE);
$nested--;
$output .= str_repeat(" ", (2 * $nested)) . ' </' . (is_string($root) ? $root : $ArrayNumberPrefix . $root) . '>' . PHP_EOL;
}
else
{
$output .= str_repeat(" ", (2 * $nested)) . ' <' . (is_string($root) ? $root : $ArrayNumberPrefix . $root) . '><![CDATA[' . $child . ']]></' . (is_string($root) ? $root : $ArrayNumberPrefix . $root) . '>' . PHP_EOL;
}
}
if ($beginning)
$output .= '</' . $name . '>';
return $output;
}
//connect to database and select database (edit yourself)
mysql_connect("localhost", "username", "password");
mysql_select_db("databasename");
//Get all posts whose status us published.
$result = mysql_query("SELECT * FROM wp_posts WHERE post_status = 'publish'");
while($row = mysql_fetch_assoc($result))
$posts[] = $row;
//convert to array and print it on screen:
echo "<pre>";
echo htmlentities(array2xml($posts, 'posts', false));
echo "</pre>";
?>
EDIT:
Right, so I created a php file called pull-posts.php and dropped the above code into the file then saved the file in the root of my localhost installation. I then changed the DB details and ran the script. Here's the error I am getting:
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'username'@'localhost' (using password: YES) in C:\inetpub\wwwroot\Xampp-1.7.7\htdocs\project\pull-posts.php on line 40
Line 40 has this on it:
mysql_connect("localhost", "root", ""); mysql_select_db("DBName");
Any thoughts? Thanks.
JOIN
the*_postmeta
table and the*
is where you insert the columns.