admin管理员组

文章数量:1334947

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 = ".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();
?>

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/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();
?>
Share Improve this question edited Aug 18, 2016 at 7:23 bueltge 17.1k7 gold badges62 silver badges97 bronze badges asked Aug 14, 2016 at 20:56 user509505user509505 412 bronze badges 2
  • If you mean adding tags and categories then maybe this can help? – birgire Commented Aug 14, 2016 at 21:51
  • 2 Have you considered using the WP REST API? Much easier than XMLRPC – Tom J Nowell Commented Aug 14, 2016 at 22:24
Add a comment  | 

1 Answer 1

Reset to default 1

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.

本文标签: xml rpcPHP XMLRPC for WordPress Adding meta tags and description