Is it somehow possible to get_post_meta
or get_post_custom
and have meta_id returned along with the meta_value
? For example:
$data = get_post_meta( $post_id, 'my_key' );
// returns this:
array( 0 => array('myvalue1', 1002 ), 1 => array( 'myvalue2', 1003 ));
The basic idea is that because there may be multiple meta_values for the same meta_key
, I would have to know the meta_id
in order to reliably update/delete the post meta.
This function worked for me:
it will return an array of objects like:
I don’t know of a Core function/method to retrieve post meta with the key. That isn’t to say that there definitively isn’t one. There may be. I don’t know everything about WordPress, I just pretend to 🙂 Or maybe it has just slipped my mind right now.
However, the fourth parameter of
update_post_meta
is to ensure that you only update the value you want to update in cases where there are multiple keys.You send the previously saved value in that fourth parameter and then only that entry is updated.
delete_post_meta
operates similarly, but with the third parameter not the fourth.add this code in your functions.php
and then call where ever you want like
It is odd that WordPress provides functions to get, update and delete meta via meta_id since v3.3, but as of v3.7 does not have any functions that return the meta id. So, using the core functions from meta.php as reference, I implemented the below functions to be able to retrieve the meta ids along with the values.
Solution
Use the custom function below
get_post_meta_db()
to get the meta key ids and values, then use WordPress’update_meta()
anddelete_meta()
to manipulate the post meta.For example:
Here are the custom function definitions:
Notes:
get_metadata_by_mid()
, by not using the"get_post_metadata"
/"get_user_metadata"
pre-filters, and not updating the cache.get_metadata_by_mid()
, this implementation does not unserialize the meta values.What you call
meta_id
here technically does not exist in WordPress. There is thepost_id
and that is also the id for all the meta (in meta sprak this is theobject_id
).Next to that there is the
meta_key
and as you rightfully write, as there can be more than one value per eachmeta_key
on the sameobject_id
, there is ambiguity because all this depends on is the order in which the database presents values here – and that only indirect, because meta values also have their cache, the object cache.So even you can see a
meta_id
in the database, it’s not further used only to have a primary key in the db.It is not part of the database result that is used within WordPress PHP userland code and therefore it is not part of the meta-cache and therefore never makes it into the meta API functions (because they access the database via the meta cache API).
Which leads to the final conclusion:
As you talk about
meta_id
and it only exists in the database (dbprefix_postmeta
table), your question can only mean to delete / update an entry in that table. Just normal SQL operations should suffice.The WordPress core function
has_meta
returns associative arrays of meta data. Each meta data array contains themeta_id
.You can reliably update and delete meta values that share the same meta key using
has_meta
withupdate_metadata_by_mid
anddelete_metadata_by_mid
.In the OPs example, to update
array('myvalue1', 1002)
and deletearray('myvalue2', 1003)
:Reference:
has_meta
is used by WordPress core to populate the form fields in the Custom Fields panel.