samedi 31 janvier 2015

Save multiple custom post fields from quick edit


Context:


A multiple custom posts and custom fields website. Since the website is rather complex, has a lot of users, a lot of editors and a lot of data, they asked me to allow them to choose which custom fields show up in the quick edit screen for each custom post type. I've managed to add the fields to the quick edit screen and they are all populated with the right values. However,...


The problem:


I don't seem to be able to save the fields from the quick edit menu. Here's the function:



add_action('save_post', 'sb_save_quick_edit_fields');

function sb_save_quick_edit_fields($post_id) {
if (
( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) ||
( !current_user_can( 'edit_post', $post_id ))
) return $post_id;
}

$qef_array = sb_get_qe_fields_options();
/* the function above returns an array from options
* with arrays of custom fields that are added to
* quick edit for each post type, in the form of:
* [
* 'post_type_1_slug' => [
* 'custom_field_1_slug',
* 'custom_field_2_slug'
* ...
* ],
* 'post_type_2_slug' => [...],
* ...
* ]
*/

if ($qef_post_type = $_REQUEST['post_type'])
foreach ($qef_array as $qef_post_type => $qef_fields) {
foreach ($qef_fields as $qef_field) {
if (array_key_exists($qef_field, $_REQUEST))
update_post_meta($post_id, $qef_field, $_REQUEST[$qef_field]);
else
delete_post_meta($post_id, $qef_field);
}
}

}


I don't have a clue about why it's not working. For all I care, it should. Does it have to do with not returning anything? In the examples I found with adding only one custom field to quick edit, the save function returned the custom fields' value upon success, but I can't return, I need to check for the rest of the custom fields. Right?





Aucun commentaire:

Enregistrer un commentaire