2

I have been trying to add WordPress posts by XMLRPC calls. The code is working but the tags and categories are not setting.

I have tried almost every solution provided in every forum.

PS: I am getting a few of the variables from another application -- that part is working correctly.

<?php 
include("lib/xmlrpc.inc");
$function_name = "wp.newPost";
$url = "http://website.com/xmlrpc.php";

$category = array('3','1');
$tags = array('tag1', 'tag2');

$client = new xmlrpc_client($url);
$client->return_type = 'phpvals';

$message = new xmlrpcmsg(
        $function_name, 
        array(
            new xmlrpcval(0, "int"), 
            new xmlrpcval("username", "string"), 
            new xmlrpcval("password", "string"), 
            new xmlrpcval(
                array(
                    "post_type" => new xmlrpcval("post", "string"), 
                    "post_status" => new xmlrpcval("draft", "string"), 
                    "post_title" => new xmlrpcval($_POST['title'], "string"), 
                    "post_author" => new xmlrpcval(1, "int"), 
                    //"post_excerpt" => new xmlrpcval("excerpt", "string"), 
                    "post_content" => new xmlrpcval($_POST['content'], "string"),
                    "category"    => new xmlrpcval($category, "int"),
                    "mt_keywords" => new xmlrpcval($tags, "string")

                    ), 
                "struct"
                )
            )
        );

$resp = $client->send($message);

if ($resp->faultCode()) echo 'KO. Error: '.$resp->faultString(); else echo "Post id is: " . $resp->value();
?>
  • If you mean adding tags and categories then maybe this can help? – birgire Aug 14 '16 at 21:51
  • 2
    Have you considered using the WP REST API? Much easier than XMLRPC – Tom J Nowell Aug 14 '16 at 22:24
0

The field for tags is mt_keywords, as reference see this ticket.

The field for the category is categories.

Important is it, that you have a array for the fields, like

$categories[0] = "cat1";
$categories[1] = "cat2";

$tags[0] = "tag1";
$tags[1] = "tag2";


$content[ 'categories' ] = $categories;
$content[ 'mt_keywords' ] = $tags;

As reference for each field it is better you use the source, not the online plattform. You should find it all in the class wp_xmlrpc_server.

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.