I have developed a custom script that uses wp_insert_post to create new posts, however i'd like to use similar code to create the same data in another wp database. Could be this done on the fly before insertion? For example instruct wordpress to point another database and then insert data? Or should I do this "manually" using raw mySQL code?

  • Have you tried this ? – mike23 Jun 21 '11 at 10:06

You could avoid SQL altogether and use the XML-RPC API. This would also let you post to remote wordpress installs too.

( note if XML-RPC is not an option, scroll further down )

If Using XML-RPC

Here's some code from a quick google search using XML-RPC to post to a remote Wordpress blog:


Here's a simpler set of examples with an explanation of the XML-RPC APIs


And here's an example from WP-Recipes using Curl and XML-RPC:


function wpPostXMLRPC($title,$body,$rpcurl,$username,$password,$category,$keywords='',$encoding='UTF-8') {
    $title = htmlentities($title,ENT_NOQUOTES,$encoding);
    $keywords = htmlentities($keywords,ENT_NOQUOTES,$encoding);

    $content = array(
        'mt_allow_comments'=>0,  // 1 to allow comments
        'mt_allow_pings'=>0,  // 1 to allow trackbacks
    $params = array(0,$username,$password,$content,true);
    $request = xmlrpc_encode_request('metaWeblog.newPost',$params);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
    curl_setopt($ch, CURLOPT_URL, $rpcurl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 1);
    $results = curl_exec($ch);
    return $results;

If afterall that XML-RPC is not for you

Perhaps a wordpress multisite might be best for you? Creating the new post would be as simple as calling switch_to_blog($blog_id); then doing your business creating the new post, before calling restore_current_blog();

If you MUST use SQL

Use this question linked to by mike23 in order to gain access via a wpdb object

  • What advanatges using this technique versus mySQL? There's quite a bit of code here, maybe little less than using mySQL... maybe this wuold prevent potential conflicts with database scheme changes over newer WP versions? – Riccardo Jun 21 '11 at 10:32
  • Indeed, fewer conflicts, and you get all the hooks filters that plugins use, and side stuff wordpress stuff that you skip by going straight for the database. – Tom J Nowell Jun 21 '11 at 11:23
  • You also gain the security of that API, whereas your SQL code hasnt been tested for years with years of bugfixing and security fixes – Tom J Nowell Jun 21 '11 at 11:24
  • Your also freed from needing the SQL database on the same server ( and its more secure than having a public facing SQL server ) – Tom J Nowell Jun 21 '11 at 11:25
  • There's also stuff like user postcounts etc to take into account – Tom J Nowell Jun 21 '11 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.