Hi, sometimes you only have available from backups an old wordpress post table sql xml file, which you might want to convert to a word doc file for say rewriting for another web site.
The following simple snippet of code I have used successfully to extract each posts title and contents (not excerpt) and add to a html file.
Starting point was a post sql xml file called bhrt.me.wp_posts.xml.. dumped from a mysql database (wp_posts table)/
Script could be called anything, but I called it ‘extract_posts.php’
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
| <?php
// replace bhrt.me.wp_posts.xml with your post table sql xml filename
$fname = "bhrt.me.wp_posts.xml";
$xml=simplexml_load_file($fname);
echo("<html>\n
<head>\n
<meta charset=\"UTF-8\">\n
<meta http-equiv=\"Content-Type\" content=\"application/xhtml+xml; charset=utf-8\"/>\n
</head>\n
<body>\n");
foreach ($xml->database->table as $table) {
$post_title="";
$post_type="";
$post_content="";
foreach ($table->column as $column) {
switch((string) $column['name']) {
case 'post_title':
$post_title = $column;
break;
case 'post_content':
$post_content = $column;
break;
case 'post_type':
$post_type = $column;
break;
}
}
if((($post_type=='post')||($post_type=='page'))&&($post_content!="")) {
if(stripos($post_content,'[contact-form')===false) {
echo "<div>\n";
echo "<p><h2>".$post_title."</h2></p>\n";
echo("<br>\n");
$post_content = preg_replace("/<img[^>]+\>/i", "", $post_content);
echo "<p>".$post_content."</p>\n";
echo "</div>\n";
echo("<br>\n");
}
}
}
echo("</body>\n
</html>");
?> |
<?php
// replace bhrt.me.wp_posts.xml with your post table sql xml filename
$fname = "bhrt.me.wp_posts.xml";
$xml=simplexml_load_file($fname);
echo("<html>\n
<head>\n
<meta charset=\"UTF-8\">\n
<meta http-equiv=\"Content-Type\" content=\"application/xhtml+xml; charset=utf-8\"/>\n
</head>\n
<body>\n");
foreach ($xml->database->table as $table) {
$post_title="";
$post_type="";
$post_content="";
foreach ($table->column as $column) {
switch((string) $column['name']) {
case 'post_title':
$post_title = $column;
break;
case 'post_content':
$post_content = $column;
break;
case 'post_type':
$post_type = $column;
break;
}
}
if((($post_type=='post')||($post_type=='page'))&&($post_content!="")) {
if(stripos($post_content,'[contact-form')===false) {
echo "<div>\n";
echo "<p><h2>".$post_title."</h2></p>\n";
echo("<br>\n");
$post_content = preg_replace("/<img[^>]+\>/i", "", $post_content);
echo "<p>".$post_content."</p>\n";
echo "</div>\n";
echo("<br>\n");
}
}
}
echo("</body>\n
</html>");
?>
Run locally via ‘php extract_posts.php > output-filename.html’ Then load the html file into your word or openoffice package – tested with libreoffice.