WordPress bulk post meta update

I am trying to add the ability to update a meta value through WordPress’s bulk post edit menu. I have it working on a single post, but I can’t figure out why it’s not working for bulk.

// Add our text to the quick & bulk edit box
add_action('quick_edit_custom_box', 'on_quick_edit_custom_box', 10, 2);
add_action('bulk_edit_custom_box', 'on_quick_edit_custom_box', 10, 2);
function on_quick_edit_custom_box($column_name, $post_type) {
    global $post;
    $postMeta = get_post_meta( $post->ID, 'wpsl_locatorID', true ); 
    if ('wpsl_locatorID' == $column_name && get_post_type() == 'location' ) {
        echo  "<fieldset class="inline-edit-col-right" style="margin-top: 0;">";
        echo    "<div class="inline-edit-col">";
        echo        "<div class="inline-edit-group">";
        echo            "<label class="alignleft">";
        echo                "<span class="title" for="wpsl_locatorID">Movie ID</span>";
        echo                "<input type="text" name="wpsl_locatorID" id="wpsl_locatorID" value="{$postMeta}" />";
        echo        "</label>";
        echo    "</div>";
        echo "</fieldset>";
    };
}

// Update the post meta
add_action( 'save_post', 'locationMetaUpdate', 10, 2 );
function locationMetaUpdate( $post_id ) {

    // Bail if we're doing an auto save
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

    // if our nonce isn't there, or we can't verify it, bail
    // if un-commented than the value won't update at all. I'm guessing b/c of WP's ajax function vs. reloading the page.
    //if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;

    // if our current user can't edit this post, bail
    if ( ! current_user_can( 'edit_posts' ) ) return;

    // Make sure that it is set.
    if ( ! isset( $_POST['wpsl_locatorID'] ) ) {
        return;
    }

    // Sanitize user input.
    $my_data = sanitize_text_field( $_POST['wpsl_locatorID'] );

    // Update the meta field in the database.
    update_post_meta( $post_id, 'wpsl_locatorID', $my_data );

};

Like I said above, this works for a single quick edit but not on bulk edit. I’ve tried to find a solution, but I’m sure it’s something simple I’m missing.

Related posts

1 comment

  1. So honestly I’m not sure why it’s now working; but all I did was condense the action like on WP’s save_post codex page.

    // Update the post meta
    add_action( 'save_post', 'locationMetaUpdate', 10, 3 );
    function locationMetaUpdate( $post_id, $post, $update ) {
            //print_r($_POST); die();
    
            // if our current user can't edit this post, bail
            if ( ! current_user_can( 'edit_posts' ) ) return;
    
            // Make sure that it is set.
            if ( isset( $_REQUEST['wpsl_locatorID'] ) ) {
                update_post_meta( $post_id, 'wpsl_locatorID', sanitize_text_field( $_REQUEST['wpsl_locatorID'] ) );
            }
    }
    

Comments are closed.