How to export WordPress posts?

How to export WordPress posts to XML or CSV? I am looking for a neat way to do it with the help of PHP.

Note: I don’t want to do it via the admin panel because I want to automate it.

Related posts

Leave a Reply

3 comments

  1. To do it from PHP, do it like this:

    1. Get all posts marked publish from the database.
    2. Export them to an array by using following array2xml function:

    .

    <?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>";
    ?>
    
  2. On your WordPress setup, take a look at wp-admin/export.php lines 28-48 (on a 3.0 setup).
    This is the code that generates the XML file downloadable in the admin. You could maybe use that in your own code (unfortunately, it’s not organized into a function, so you’ll have to do some copy-paste-ing).

    Also, you could automate the downloading of http://yourblog/wp-admin/export.php?download, as this URI would always redirect to a fresh XML export. You’ll have to deal with inputting your credentials for that, though.

  3. Well according to the WordPress blog…

    You’ll find the export and import
    options now under “Manage” in your
    blog admin area. The XML format is an
    extended version of RSS 2.0, and it
    will be built into the next
    downloadable release of WordPress
    (2.1).