wp.​​editPost' API Wordpress'不编辑自定义字段

时间:2015-04-20 15:12:36

标签: php wordpress api

当我尝试使用wp.editPost编辑custom_fields时。仅编辑其他字段,但不编辑自定义字段。再次创建自定义字段(重复字段),但必须进行编辑。

我在寻找:http://codex.wordpress.org/XML-RPC_WordPress_API/Posts#wp.editPost

我的自定义字段数组是:

    $content = array(
        'post_id' => (int)$idPostWp,
        'title' => $modificarPostWpDecode['title'], //ok edit
        'description' => $modificarPostWpDecode['content'], //ok edit
        'categories' => $modificarPostWpDecode['category'], //ok edit
        'custom_fields' => array(
            array('key' => 'precio', 'value' => $modificarPostWpDecodeCustom['price']), // no edit, fields will be create again
            array('key' => 'category', 'value' => $modificarPostWpDecodeCustom['category']), // no edit, fields will be create again
            array('key' => 'estrenar', 'value' => $modificarPostWpDecodeCustom['new']), // no edit, fields will be create again
            array('key' => 'currency', 'value' => $modificarPostWpDecodeCustom['currency']), // no edit, fields will be create again
            array('key' => 'search', 'value' => $modificarPostWpDecodeCustom['search']) // no edit, fields will be create again
            )
    );

我对wordpress的呼吁是:

    $params = array(1, WPUSER, WPPASS, (int)$idPostWp, $modificarPostWpDecode);
    $request = xmlrpc_encode_request('wp.editPost', $params, array('encoding' => 'UTF-8', 'escaping' => 'markup'));

非常感谢!

1 个答案:

答案 0 :(得分:1)

如上所述here,您必须传递自定义字段的ID才能编辑字段,而不是密钥,最终会创建副本。

所以你需要做两个请求,除非你已经知道自定义字段ID。一个请求获取所有自定义数据,循环遍历字段,并将相应的ID收集到要更新的字段。第二个请求将更新使用字段ID指定的字段,而不仅仅是密钥。

ID的集合可能类似于以下

$custom_fields_to_edit = array(
    'key1' => null,
    'key2' => null
    );

foreach($post->custom_fields as $custom){
    if (array_key_exists($custom->key, $custom_fields_to_edit)){
        $custom_fields_to_edit[$custom->key] = $custom->id;
    }
}

使用$post程序收集wp.getPost

然后您可以像以前一样继续操作,并对您的代码进行以下修改。

'custom_fields' => array(
    array('id' => $custom_fields_to_edit['key1'], 'key' => 'key1', 'value' => $modificarPostWpDecodeCustom['key1']),
    array('id' => $custom_fields_to_edit['key2'], 'key' => 'key2', 'value' => $modificarPostWpDecodeCustom['key2'])
    )
相关问题