Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

In my function for saving custom field values I add a few checks to prevent the values from being cleared during an autosave or a quick edit.

add_action('save_post', 'save_my_post');
function save_my_post($post_id)
{
    // Stop WP from clearing custom fields on autosave,
    // and also during ajax requests (e.g. quick edit).
    if ((defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) || (defined('DOING_AJAX') && DOING_AJAX))
        return;

    // Clean, validate and save custom fields
    $myfield = ( ! isset($_POST['myfield'])) ? '' : strval($_POST['myfield']);
    update_post_meta($post_id, 'myfield', $myfield);
}

It appears, though, that the custom fields are still cleared in the case of a bulk edit. The DOING_AUTOSAVE and DOING_AJAX checks don't apply to bulk edits.

I realize that you could simply not call update_post_meta if the applicable $_POST variables are not set. That wouldn't work in the case of checkboxes, though.

Ideally, a simple check to determine whether we're in a bulk edit or not would do the job. Ideas?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

You can check for a bulk edit by looking at the bulk_edit variable in $_GET or $_POST. Bulk edits are typically GET requests as far as I investigated them.

Note that $_REQUEST takes both GET and POST data into account. In wp-admin/edit.php they also do an isset() check for $_REQUEST['bulk_edit'].

function save_my_post($post_id)
{
    // Do nothing during a bulk edit
    if (isset($_REQUEST['bulk_edit']))
        return;

    // ...
}
share|improve this answer
1  
Thank you for sharing this. Helped me in a similar problem. –  kingkool68 Dec 21 '11 at 16:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.