Support » Fixing WordPress » Create Posts using Import>RSS/WXR/XML and passing Values to Custom Fields

  • My problem is simple. I need to make a lot of posts containing image url’s for a “photo gallery” page. I’ve read up a lot on creating posts from RSS feeds and imports. I’ve been successful at creating posts but can not somehow pass a value into a custom type. The gallery needs two url’s, one for the image thumbnail and one for the full size. The code I’m using looks like this;

    <?xml version="1.0" encoding="UTF-8" ?>
    <!-- generator="WordPress/3.2.1" created="2011-11-14 01:56" -->
    <rss version="2.0"
    		<title>Pavers 1</title>
    		<title>Swimming Pools 1</title>
    		<Category>Swimming Pools</Category>

    I think I’m going in the right direction with this and would greatly appreciate the ability to create these posts this way. I have almost 1000 images I need to add to an individual post complete with descriptions and categories. If I were able to “batch” them then I’d save myself a lot of time and remove the high probability of human error.
    I’d really be grateful to any help or a steer in the right direction.

Viewing 1 replies (of 1 total)
  • I relentlessly searched and read until I found an answer! Turns out, the right thing to use is actually MetaWeblog API and XMLRPC, which you’ll have to activate as it is disabled by default.
    First thing, visit the Settings menu in your admin panel and navigate to “Writing”. Scroll down until you see the XML-RPC check box under “Remote Publishing”. Once this is saved your pretty much all set.

    You’ll have to export posts and then read through that xml file to get the meta-names of your specific custom fields.
    Here’s the code I used:

     $BLOGURL = "your.wordpress.root/folder";
        $USERNAME = "your_uesername";
        $PASSWORD = "your_password";
        function get_response($URL, $context) {
        if(!function_exists('curl_init')) {
        die ("Curl PHP package not installed\n");
        /*Initializing CURL*/
        $curlHandle = curl_init();
        /*The URL to be downloaded is set*/
        curl_setopt($curlHandle, CURLOPT_URL, $URL);
        curl_setopt($curlHandle, CURLOPT_HEADER, false);
        curl_setopt($curlHandle, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
        curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $context);
        /*Now execute the CURL, download the URL specified*/
        $response = curl_exec($curlHandle);
        return $response;
        /*Creating the metaWeblog.newPost request which takes on five parameters
        /*The title of your post*/
        $title = "Sample Post Title with Custom Fields";
        /*The contents of your post*/
        $description = "a collection lorem ipsums";
        /*Forming the content of blog post*/
        $content['title'] = $title;
        $content['description'] = $description;
        $content['categories'] = array("Uncategorized");
        /*Pass custom fields*/
        $content['custom_fields'] = array(
        	array( 'key' => 'your_custom_feild_meta-key', 'value' => 'place whatever value your custom field requires here' )
        /*Whether the post has to be published, false means it will be created as a draft*/
        $toPublish = false;
        $request = xmlrpc_encode_request("metaWeblog.newPost",
        array(1,$USERNAME, $PASSWORD, $content, $toPublish));
        /*Making the request to wordpress XMLRPC of your blog, you may have to change this to the correct path for your xmlrpc.php file*/
        $xmlresponse = get_response($BLOGURL."/xmlrpc.php", $request);
        $response = xmlrpc_decode($xmlresponse);
       /*Printing the response on to the console*/
       /*If it works, you'll see a number followed by :Post ID*/
       	echo ":Post ID"; print_r($response);
        echo "\n";

    This is it! Now you can easily automate the creation of this file with some php, javascript, perl… whatever you want! Theres only a few variables to change.

Viewing 1 replies (of 1 total)
  • The topic ‘Create Posts using Import>RSS/WXR/XML and passing Values to Custom Fields’ is closed to new replies.