Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Quick Edit/Bulk Edit clears all custom field values! WordPress

  • SOLVED

Hi,

I've searched for a solution for this problem on forums etc. but I just can't find a solution. This looks like a bug but I think it was, more people should have encountered it, and since it is rather serious, it should've been fixed in WP 3.01 (which is the version I am using).

The problem is the following;

I've set-up several custom write panels for editing posts. Several custom fields are saved with the post. This includes a reference to a video file, video thumbnail among other custom data.

When the post is saved/updated via the Edit Post screen there is no problem. The custom fields are saved as they should. However, when a post is edited using the Quick Edit or Bulk Edit feature (e.g. changed the category, adding some tags), <strong>all the custom field values of that post are deleted!
</strong>

First I figured a "workaround" by just hiding the Quick Edit option (via CSS) on the post list, but this does not work for Bulk Edit. I can't figure out what I am doing wrong here and I need some solutions quick!

What might be relevant here is the code I use to save custom field values of posts:


function xx_save_metabox( $post_id ) {
// check if this is autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return $post_id;
if( !current_user_can( 'edit_post', $post_id ) ) // check permissions
return $post_id;
$fields = array(
'xx_stdvideo_url',
'xx_hdvideo_url',
'xx_thumb_url');
foreach( $fields as $field ) {
if( isset($_POST[$field]) && $_POST[$field] != '' )
update_post_meta( $post_id, $field, $_POST[$field] );
else
delete_post_meta( $post_id, $field, get_post_meta( $post_id, $field, true ) );
}
}
add_action( 'save_post', 'xx_save_metabox' );


(the actual function prefix is replaced by xx)


Answers (3)

2010-08-18

Utkarsh Kukreti answers:


function xx_save_metabox( $post_id ) {
// check if this is autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return $post_id;
if( !current_user_can( 'edit_post', $post_id ) ) // check permissions
return $post_id;
$fields = array(
'xx_stdvideo_url',
'xx_hdvideo_url',
'xx_thumb_url');
foreach( $fields as $field ) {
if( isset($_POST[$field]) && $_POST[$field] != '' )
update_post_meta( $post_id, $field, $_POST[$field] );
else if( isset($_POST[$field]) )
delete_post_meta( $post_id, $field, get_post_meta( $post_id, $field, true ) );
}
}
add_action( 'save_post', 'xx_save_metabox' );

2010-08-18

Oleg Butuzov answers:

if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'){
return $post_id;
}


one more rule


jedw comments:

The problem persists after adding this rule. Considering your quick response you've dealt with this particular problem before, am I missing something here?


function xx_save_metabox( $post_id ) {
// check if this is autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return $post_id;
if( !current_user_can( 'edit_post', $post_id ) ) // check permissions
return $post_id;
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest')
return $post_id;
$fields = array(
'xx_stdvideo_url',
'xx_hdvideo_url',
'xx_thumb_url');
foreach( $fields as $field ) {
if( isset($_POST[$field]) && $_POST[$field] != '' )
update_post_meta( $post_id, $field, $_POST[$field] );
else
delete_post_meta( $post_id, $field, get_post_meta( $post_id, $field, true ) );
}
}
add_action( 'save_post', 'xx_save_metabox' );


jedw comments:

Can you elaborate on the code you suggested? What does it?


Oleg Butuzov comments:

yeas your right i had got this issue before

the code you insert is correct one. what it dose...

when post saved (from ajax query) its hook your triger but there is no POST data that you requeire... thats why its delete values from custom fields. because thay not found.

what i did i just provide additional rule that if thats a ajax request we only return the post_id thats all...

can you confrm that this

$fields = array(

'xx_stdvideo_url',

'xx_hdvideo_url',

'xx_thumb_url');


custom fields are lost after the ajax saving? because thats quite intresting stuff in this case.


Oleg Butuzov comments:

<strong>all the custom field values of that post are deleted!</strong>

all or just a that values that you had add?

'xx_stdvideo_url',
'xx_hdvideo_url',
'xx_thumb_url'

2010-08-18

Jonah Schulte answers:

It seems like the part where you delete the post meta if it's not set in the $_POST array might be causing the issue...


delete_post_meta( $post_id, $field, get_post_meta( $post_id, $field, true ) );


Maybe try without that and see if the problem persists? I'm not sure why that's there, as you can just delete the post meta on the edit screen if desired...