I have about 30,000 very short posts to make in Wordpress. I built them using a SQLlite database and python script, and was uploading them via the wordpress_xmlrpc library in python. But my site goes down after 100-200 posts, because the server thinks this is a DoS attack.
My server is a linux box to which I have SSH access. I'm looking for a way to easily upload the posts into Wordpress more directly, say by interacting directly with its database, or using a local process that happens directly on the server. Can anyone offer any ideas?
I have tried to do the same by interacting directly with the database through Python script and it worked like a charm for me. I was using MySQL database.
For this you need to ssh into the server where Wordpress site and your database is hosted. And run the following script there:
Perquisite to run this script:
a. All the posts should be in different files within a single directory.
b. Each file should contain post title in first line and post content in rest of the lines.
#!/usr/bin/env python
import MySQLdb
import fnmatch
import os
#List to contain all the post files
my_match = []
#Gather post files in above list
for file in os.listdir("<path of the directory where post files remains>"):
if fnmatch.fnmatch(file, '.*'):
print(file)
continue
my_match.append(file)
print my_match
#Make database connection
conn = MySQLdb.connect(host= "localhost", user="<username>", passwd="<password>", db="<database name>")
x = conn.cursor()
print x
for fl in my_match:
new_file = "<path to the directory where post files remains>/" + fl
with open(new_file) as f:
heading = f.readline().strip()
content = f.read()
print heading
url = heading.replace(" ", "-")
print url
#try db query, change according to your database and tables
try:
x.execute("""INSERT INTO wp_posts (post_author, post_date, post_content, post_title, post_name) VALUES (3, "2017-03-28 20:24:12", %s, %s, %s)""",(content, heading, url))
conn.commit()
print "Done! :)"
except:
conn.rollback()
print "Oops, not done :("
conn.close()
Found another way, in case anyone else stumbles on this. Dump all the posts to a csv file and use a "csv to post" widget. I'm using Ultimate CSV Importer Free. Very simple.