wp.editPost ‘API WordPress’ doesn’t edit custom fields

When I try to edit custom_fields with wp.editPost. Only edit the other fields, but not custom fields. Custom fields are created again(repeat fields), but will have to be edited.

I am looking: http://codex.wordpress.org/XML-RPC_WordPress_API/Posts#wp.editPost

Read More

My array with custom fields is:

    $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
            )
    );

My call to wordpress is:

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

Thank a lot!

Related posts

Leave a Reply

1 comment

  1. As noted here, you must pass the custom field’s ID to edit the field, rather than the key, which would end up creating a duplicate.

    So you need to do two requests, unless you already know the custom field IDs. One request to get all the custom data, loop through the fields, and collect the appropriate IDs to the fields you want to update. The second request will update the fields, specified using the field IDs, not just the key.

    The collection of IDs can look similar to the following

    $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;
        }
    }
    

    where $post has been collected using the wp.getPost procedure.

    You can then proceed as previously, with the following modification to your code.

    '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'])
        )