Insert post in another database

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?

Related posts

Leave a Reply

1 comment

  1. 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:

    http://en.forums.wordpress.com/topic/great-code-for-remote-posting?replies=5

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

    http://life.mysiteonline.org/archives/161-Automatic-Post-Creation-with-Wordpress,-PHP,-and-XML-RPC.html

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

    http://www.wprecipes.com/post-on-your-wordpress-blog-using-php

    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(
            'title'=>$title,
            'description'=>$body,
            'mt_allow_comments'=>0,  // 1 to allow comments
            'mt_allow_pings'=>0,  // 1 to allow trackbacks
            'post_type'=>'post',
            'mt_keywords'=>$keywords,
            'categories'=>array($category)
        );
        $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);
        curl_close($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