Make WordPress Core

Opened 9 years ago

Closed 5 years ago

#32605 closed enhancement (duplicate)

Add short-circuit filters to `*_metadata_by_mid()` functions

Reported by: dlh's profile dlh Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Options, Meta APIs Keywords: has-patch reporter-feedback
Focuses: Cc:

Description

Currently it's possible to short-circuit get_metadata(), update_metadata(), and delete_metadata(), but not their _by_mid() variants.

The attached patch would add comparable filters to get_metadata_by_mid(), update_metadata_by_mid() and delete_metadata_by_mid().

Attachments (1)

32605.patch (4.1 KB) - added by dlh 9 years ago.

Download all attachments as: .zip

Change History (12)

@dlh
9 years ago

#1 @johnbillion
9 years ago

  • Keywords has-patch reporter-feedback added

Out of interest, what's your use case?

#2 @dlh
9 years ago

In my case, a plugin was calling wp_xmlrpc_server::set_custom_fields() in a way that would lead to delete_metadata_by_mid() in some instances when I didn't want the meta deleted.

That said, I was also able to find a workaround without the filter.

#3 @Er_eri
9 years ago

Can you please make a simple save function as it is for update_post_meta()? I need one function for ajax updating meta_id. Please, i need just one simple example for updating the meta_id. Thanks in advance

For example like this:

add_action( 'wp_ajax_my_action', 'isave_meta' );
function isave_meta( $post_id ) {

if( wp_verify_nonce( $_REQUEST['nonce'], 'update_meta' ) ) {
if( update_post_meta( $_POST['post_id'], 'my_meta_key', sanitize_text_field( $_POST[ 'my_meta_key' ] ) ) )
}
else { wp_die( '-1'); }	
}

#4 follow-up: @sc0ttkclark
9 years ago

@Er_eri you'll want to just use update_metadata_by_mid( 'post', absint( $_POST['post_id'] ), 'my_meta_key', sanitize_text_field( $_POST['my_meta_key'] ) ); instead here, it's already there for you to use it.

https://core.trac.wordpress.org/browser/trunk/src/wp-includes/meta.php#L596

You don't need new functions for update_post_meta_by_mid etc, this is definitely an edge case.

#5 in reply to: ↑ 4 @Er_eri
9 years ago

Replying to sc0ttkclark:

@Er_eri you'll want to just use update_metadata_by_mid( 'post', absint( $_POST['post_id'] ), 'my_meta_key', sanitize_text_field( $_POST['my_meta_key'] ) ); instead here, it's already there for you to use it.

https://core.trac.wordpress.org/browser/trunk/src/wp-includes/meta.php#L596

You don't need new functions for update_post_meta_by_mid etc, this is definitely an edge case.

Thank you for your reply. I'm not so good at coding. I'm trying to make it work but without success.

Here is the simplest one. Is this the right one?

add_action( 'wp_ajax_my_action', 'isave_meta' );
function isave_meta( $post_id ) {

update_metadata_by_mid( 'post', absint( $_POST['post'] ), 'my_meta_key');
}

And the JS:

		var data = {
			action:         'my_action',
			post_id:         $('#post_ID').val(),
			my_meta_key:     $('#the_value').val(),
			
		};
		$.post(ajaxurl, data, function(response) {
		alert('Got this from the server: ' + response);
		});

#6 @sc0ttkclark
9 years ago

This isn't the place to seek support like that, try in the support forums at https://wordpress.org/support/

To answer your question though, you don't need to use update_metadata_by_mid if you don't have a meta_id. In your code, you don't, so you would only need to use the regular update_post_meta as you ahd it before.

#7 @Er_eri
9 years ago

Im using multiple values for one meta_key, that's why i need to update the meta_id not the key. update_post_meta is updating all values, and i dont want that.

I have 2 days searching and asking around and still i cant get it work just a simple code for updating the meda_id.

As for your reply, my code it has the meta_id. I just pasted your code.

Last edited 9 years ago by Er_eri (previous) (diff)

#8 follow-up: @sc0ttkclark
9 years ago

Your code only has post_id, meta_key, and the meta_value, there is nothing in it that covers meta_id. You should still open up a forum topic, feel free to send it to me and we can continue there.

#9 in reply to: ↑ 8 @Er_eri
9 years ago

Replying to sc0ttkclark:

Your code only has post_id, meta_key, and the meta_value, there is nothing in it that covers meta_id. You should still open up a forum topic, feel free to send it to me and we can continue there.

Would be great. How can i send it to you?

#10 @sc0ttkclark
9 years ago

Just mention my username in your forum post @sc0ttkclark

#11 @dlh
5 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

It looks to me like #44467 has added filters with the same effects as those discussed here.

Note: See TracTickets for help on using tickets.