wp.editPost 'API Wordpress' doesn't edit custom fields - php

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
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!

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

Related

Get value from a multidimensional array based on a parameter

I am trying to show a form based on a variable in the url. This is my array:
$blocks = array(
'oc1' => array(
'slugid' => 'oc1',
'title' => 'One Column 1',
'desc' => 'Block with text',
'values' => array(
'textarea',
'title'
)
),
'oc2' => array(
'slugid' => 'oc2',
'title' => 'One Column 2',
'desc' => 'Block with button',
'values' => array(
'title'
)
)
);
Now I want to show form fields based on the values array. So if my url is test.php?b=oc1 it should show the textarea field. If test.php?b=oc2 it should not because textarea is not added to the values array.
I've tried a lot of answers I found on StackOverflow but I can't get it to work.
So if anyone knows how to do this I would be very very grateful.
Check if is defined the $_GET variable (if you have not done it before) and using the in_array function check if textarea value exist in your two-dimensional array.
if (isset($_GET['b']) && in_array('textarea', $blocks[$_GET['b']]['values']))
{
echo 'textarea';
}

Find key type of second array by matching key id of second array with key id of first array

Maybe the type of my question is not quite clear, but i don't know a better way to explain what i need. Here is the thing i am working on some custom page in WordPress and i have input fields in an array. The input fields in that array match the id's of second array, but i actually need to get the type field of second array that match the id of first array.
Here is example of first array
$first_array = array(
'sample_text' => 'some text here',
'sample_textarea' => 'some text here in textarea field',
'sample_upload' => 'http://somelink.com/someimage.jpg'
);
And here is the second array.
$second_array = array(
array(
'type' => 'upload',
'id' => 'sample_upload',
'title' => 'Sample upload button'
),
array(
'type' => 'textfield',
'id' => 'sample_text',
'title' => 'Sample text field'
),
array(
'type' => 'textarea',
'id' => 'sample_textarea',
'title' => 'Sample textarea field'
),
);
So basically the second array is used in first place to generate an input fields in front-end, but upon form submit the form submits an array which looks like first example, so now on first array i need to loop for each input and match the id's of second and first array but when id's are match i need to take the type field and apply filter with that type field name.
So basically
// loop through inputs in the array
foreach( $first_array as $key => $value ) {
// Now the first $key would be 'sample_text'
// How to search $second_array for 'id' with 'sample_text'
// And if that 'id' exists, take the 'type' field and apply filter named same as that 'type' field
}
But i don't know exactly how i would loop through second array and get 'type' based on 'id'
I would add useful keys to the second array, like so:
$second_array = array(
'sample_upload' => array(
'type' => 'upload',
'id' => 'sample_upload',
'title' => 'Sample upload button'
),
'sample_text' => array(
'type' => 'textfield',
'id' => 'sample_text',
'title' => 'Sample text field'
),
'sample_textarea' => array(
'type' => 'textarea',
'id' => 'sample_textarea',
'title' => 'Sample textarea field'
),
);
When looping over the first array, you can use the known keys to access the second array.
foreach ($first_array as $key => $value) {
$sa = $second_array[$key];
}
That loop would usually have some more error-checking code in there, e.g. to make sure that the key exists, which has been left out for the sake of brevity.
Edit: array_filter() won't work as original though, so you could use the following instead
function checkKey($item, $k, $key) {
return $item['id'] === $key;
}
foreach( $first_array as $key => $value ) {
// Now the first $key would be 'sample_text'
// How to search $second_array for 'id' with 'sample_text'
$sa = $second_array[array_walk($second_array, 'checkKey', $key)];
// And if that 'id' exists, take the 'type' field and apply filter named same as that 'type' field
}

How can I create additional required fields for an object using jSON in Yii

I have a project to create a flexible information system. In this project I use a relational database (MySQL) and the Yii framework. Sometimes I have objects that can collect different information according to the id of the objecttype.
Basically, I have an object, which has an objecttype. In the objecttype table i have an info field which will contain a jSON with the description of the additional fields that I will require from the user at the moment of the new object creation.
http://www.bedoya.co/screenshots/object-objecttype-relation.png
Ok, now, with PHP I will collect the contents from objecttype_info (stored there as a jSON) and I will generate an array similar to this one:
<?php
$x = array(
'name' => 'Cool name to put in the fieldset label of this object type',
'fields' => array( // List of additional fields that describe this object
'field1' => array(
'label' => 'Field1 label',
'type' => 'text',
'htmlOptions' => array(
'class' => 'field1-class'
'id' => 'field1-id'
'required' => true
)
),
'campo2' => array(
'label' => 'Field2 label',
'type' => 'number',
'htmlOptions' => array(
'class' => 'field2-class'
'id' => 'field2-id'
'required' => false
)
),
)
);
Now, I get the required values with the easy function:
<?php
$x = ObjectType::model()->findByPk( $objecttype_id )->attributes[ 'objecttype_info' ];
?>
I don't know how to set the validation rules for the fields obtained from the objecttype. Any ideas? Am I doing this right?

Zend_Validate_Db_RecordExists or empty value

I've got a (hopefuly) simple task, and my google fu has failed me. Basically, I've got a form with a select which contains an empty value and then number of ids given content can belong to. What I want to do is - validate if the given ids exist, but only if a value is set. This:
$field = new Zend_Form_Element_Select('field');
$field->addValidator(
new Zend_Validate_Db_RecordExists(
array(
'table' => 'categories',
'field' => 'id'
)
));
takes care of the checking if the given id exists, but I'm not able to find any way to omit this if value is empty. One way to do this would be to move this logic to isValid method, but I'm hoping there's nicer way to accomplish this task.
Try to set this form element as not required:
$field->setRequired(false);
When element is not required and is not filled, validators queue won't be run.
Quick example which works for me:
// Zend_Form form body
$this->addElement('select', 'category', array(
'label' => 'Choose category',
'required' => false,
'multiOptions' => array(
null => 'No category selected',
'1' => 'One',
'2' => 'Two',
),
'validators' => array(
array('Db_NoRecordExists', false, array(
'schema' => 'public',
'table' => 'category',
'field' => 'id',
)),
),
));

How do I save custom node types in Drupal 7

I've created a custom node type in Drupal 7, using the hook_node_info method in the install file:
// declare the new node type
function foo_node_info ( ) {
return array(
'foo' => array(
'name' => t('Foo entry'),
'base' => 'node_content',
'description' => t('For use to store foo entries.'),
));
} // END function foo_node_info
and I'm trying to save that type in the module file using the following code:
// INSERT the stuff
node_save(node_submit((object)array(
'type' => 'foo',
'is_new' => true,
'uid' => 1,
'title' => 'Title, blah blah blah',
'url' => 'url here, just pretend',
'body' => '<p>test</p>',
)));
My issue, is that the url, and body fields aren't saving. Any idea what I'm doing wrong?
So, after a ton of digging, it turns out that the way I was entering the custom fields in the node_save was wrong. The node_save needs to look like the following:
node_save(node_submit((object)array(
'type' => 'foo',
'is_new' => true,
'uid' => 1,
'title' => 'the title',
'url' => array(
'und' => array(array(
'summary' => '',
'value' => 'url value',
'format' => 2,
))),
'body' => array(
'und' => array(array(
'summary' => '',
'value' => 'the body goes here',
'format' => 2,
))),
)));
Notice that for the custom fields, the array structure has to match what was previously going on with CCK (pretty much exactly). The first key in the array describing the field value is the language for the content.
I've used 'und' here only because that's what I saw going into the database when entering the data through a form.

Categories