WordPress.org

Make WordPress Core

Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#12414 closed defect (bug) (fixed)

delete_post_meta not working correctly when used with save_post

Reported by: studiograsshopper Owned by:
Milestone: 3.0 Priority: normal
Severity: normal Version: 3.0
Component: Administration Keywords:
Focuses: Cc:

Description

I think this is a bug because the same code works fine in 2.9.2.

I'm using add_metabox hooked to admin_menu to manage certain custom fields in the Post and Page Editor. A further function, hooked to save_post, gathers the $_POST items from my metabox, sanitises the data and then adds, updates or deletes postmeta using add_post_meta(), update_post_meta() and delete_post_meta() as required. If a metabox is empty, eg when the user blanks out a metabox field, the function uses this:

delete_post_meta($post->ID, $key);

to delete the postmeta meta_key from the db where the meta_value is empty.

The problem is that delete_post_meta() is no longer working under the latest nightly build (but works fine in WP 2.9.2). No errors are thrown, but the postmeta meta_key is not removed from the db.

As mentioned, this same code works perfectly on WP 2.9.2. I did a search on trac to see if I could find any tickets relating to this, but either my searching was ineffective, or no one else has reported this.

add_post_meta() and update_post_meta() work as expected.

This bug affects several plugins and themes which use this kind of functionality.

For the sake of completeness, I've attached one of the files from a plugin of mine which is affected by this.

Attachments (2)

dfcg-admin-metaboxes.php (8.4 KB) - added by studiograsshopper 6 years ago.
Sample code which shows problem in WP 3.0 alpha
delete-postmeta-tester.php (4.8 KB) - added by studiograsshopper 6 years ago.
Test plugin to demonstarte issue

Download all attachments as: .zip

Change History (7)

@studiograsshopper6 years ago

Sample code which shows problem in WP 3.0 alpha

comment:1 follow-up: @dd326 years ago

  • Component changed from General to Administration
  • Keywords reporter-feedback added; needs-patch removed
  • Priority changed from high to normal
  • Severity changed from major to normal

This is working fine for me in a test run:

var_dump(get_post_meta(1, 'key') );
array(0) { } 

var_dump(add_post_meta(1, 'key', 'value'));
bool(true)

var_dump(get_post_meta(1, 'key') );
array(1) { [0]=> string(5) "value" }

var_dump( delete_post_meta(1, 'key') );
bool(true)

var_dump(get_post_meta(1, 'key') );
array(0) { }

Can you link to the plugin in question which results in the bug? If theres a bug, Its not due to the functions, rather something along the line where you're calling it.

Resetting priority/severity until a problem is confirmed.

comment:2 in reply to: ↑ 1 @studiograsshopper6 years ago

Replying to dd32:

This is working fine for me in a test run:

var_dump(get_post_meta(1, 'key') );
array(0) { } 

var_dump(add_post_meta(1, 'key', 'value'));
bool(true)

var_dump(get_post_meta(1, 'key') );
array(1) { [0]=> string(5) "value" }

var_dump( delete_post_meta(1, 'key') );
bool(true)

var_dump(get_post_meta(1, 'key') );
array(0) { }

Can you link to the plugin in question which results in the bug? If theres a bug, Its not due to the functions, rather something along the line where you're calling it.

Resetting priority/severity until a problem is confirmed.

Here's a test plugin. Install and activate, then add data in the new metabox, publish post/page. Then blank out one of the fields in the metabox and click Update.
Upon refresh, you should see that the metabox correctly reflects what you've just done (one field has content, the other doesn't). Do this with both a Page and a Post. Check your wp_postmeta table and you'll see that for the Page, the "empty" postmeta meta_key is still in the db, whereas for the Post the empty postmeta meta_key has been deleted correctly.
At least this narrows it down to only showing this behaviour with Pages.
So, this seems to be an issue wi

@studiograsshopper6 years ago

Test plugin to demonstarte issue

comment:3 follow-up: @dd326 years ago

(In [13496]) Fix Metadata deletion. See #12414 for trunk

comment:4 @dd326 years ago

  • Keywords reporter-feedback removed
  • Resolution set to fixed
  • Status changed from new to closed

I thought this code was in 2.9 too, But it isnt.

The code in question was setting $object_id to "1" through some unfortunate logic. Didnt pick that up in the testing.

comment:5 in reply to: ↑ 3 @studiograsshopper6 years ago

Replying to dd32:

(In [13496]) Fix Metadata deletion. See #12414 for trunk

Works for me. Perfect! Thanks. :-)

Note: See TracTickets for help on using tickets.