3
votes

I'm trying to add multiple categories to a Wordpress (3.3.1) post via XMLRPC.

This is my code (it works fine, please read below):

<?
error_reporting(E_ALL);
ini_set('display_errors', '1');


require_once("IXR_Library.php.inc"); // http://www.hurricanesoftwares.com/php_uploads/IXR_Library.txt

$client->debug = true; //Set it to false in Production Environment

$title="Blog Title5"; // $title variable will insert your blog title 
$body = "teste xmlrpc <a href='http://www.teste.com'>teste.com</a>";

$category="DVDSCR, Telesync"; // Comma seperated pre existing categories. Ensure that these categories exists in your blog.
$keywords="keyword1, keyword2, keyword3";

$customfields=array('key'=>'Author-bio', 'value'=>'Autor Bio Here'); // Insert your custom values like this in Key, Value format


    $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),
        'custom_fields' =>  array($customfields)


    );

// Create the client object
$client = new IXR_Client('http://127.0.0.1/xmlrpc.php');

 $username = "admin"; 
 $password = "password"; 
 $params = array(0,$username,$password,$content,true); // Last parameter is 'true' which means post immideately, to save as draft set it as 'false'

// Run a query for PHP
if (!$client->query('metaWeblog.newPost', $params)) {
    die('Something went wrong - '.$client->getErrorCode().' : '.$client->getErrorMessage());
}
else
    echo "Article Posted Successfully";

?>

The error:
If I try to add more than one category the post category is set to uncategorized (default).

I've already tried this:

$category = "telesync, dvdscr";

and this:

$category =array('telesync','dvdscr');

How can I add more than one category to the post? Thank you all!

2

2 Answers

2
votes

I've found the answer after testing some other options like:

'categories'=>array("telesync", "1080p"),

The $content variable would look like this:

$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("telesync", "1080p"), // I've typed the categories directly here.
    'custom_fields' =>  array($customfields)


);
0
votes

I know this is a bit late but for those who encounter the same issue, the first guess was the best solution (instead of typing the categories directly, it is best to pass them as variables):

$category =array('telesync','dvdscr');

We just have to remove the 'array' on the categories=>array($category) since we already declared $category as an array. So instead of:

'categories'=>array($category),

use:

'categories'=>$category,

and it should work.