Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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"
xmlns:excerpt="http://wordpress.org/export/1.1/excerpt/"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:wp="http://wordpress.org/export/1.1/">
<item>
    <title>Pavers 1</title>
    <description></description>
    <Category>Flooring</Category>
    <Category>Pavers</Category>
<wp:postmeta>
    <wp:meta_key>port_large_image_url</wp:meta_key>
    <wp:meta_value>images/portfolio/architectural.elements/Pavers.1.jpg</wp:meta_value>
</wp:postmeta>
</item>
<item>
    <title>Swimming Pools 1</title>
    <description></description>
    <Category>Flooring</Category>
    <Category>Swimming Pools</Category>
<wp:postmeta>
    <wp:meta_key>port_large_image_url</wp:meta_key>
    <wp:meta_value>images/portfolio/architectural.elements/Swimming.Pool.Decks.1.jpg</wp:meta_value>
</wp:postmeta>
</item>
</channel>
</rss>

I confirmed the meta_key's when I exported posts and reviewed the xml file generated. So I know I've got to be close! Evidently I'm missing something.

One question to go along with this; if I understand this correctly a Custom Field are the individual "meta-key's"? Is this right?

I have almost 1000 images I need to create individual posts for, 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.

share|improve this question

1 Answer 1

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:

<?php
$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
blogid,
username,
password*/

/*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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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