How can I update multiple fields/Custom fields for the same post using only one call?
Everything I came across are cases of multiple calls of “update_post_meta”, for each of the fields.
Is there a way to pass the data using an array, to update multiple post meta values (for the same post) using only one call?
I am trying the following but it doesn’t seem to work, is it even possible to update multiple meta using the “wp_update_post”?
$New_Meta = array
(
'Client_Name' => $Client_Name_Edit,
'Client_Type' => $Client_Type_Edit,
);
wp_update_post(array(
'ID' => $Post_ID,
'post_name' => $Client_Name_Edit,
'post_title'=> $Client_Name_Edit,
'meta_input'=> $New_Meta,
));
}
Many thanks in advance
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
EDIT: Original answer was inaccurate as you can do this with wp_update_post and the meta_input field
As per the updated question and comments this is possible with wp_update_post (or wp_insert_post for a new record) using the meta_input key on the array, e.g.:
$metaValues = array(
'key1' => 'value1',
'key2' => 'value2',
// ... as many key/values as you want
);
wp_update_post(array(
'ID' => $postId,
'meta_input'=> $metaValues,
));
Original Answer (Works but no point doing it like this given above option)
Nope. The docs clearly show the function doesn’t support that.
Obviously you can use an array to pass through many values if you have a bunch of things to do at the same time.
This is a PHP programming question, not a WordPress question, but the code would look something like this:
$postId = 1234;
// Perhaps you have multiple key value pairs, like this:
$metaValues = array(
'key1' => 'value1',
'key2' => 'value2',
// ... as many key/values as you want
);
// Set all key/value pairs in $metaValues
foreach ($metaValues as $metaKey => $metaValue) {
update_post_meta($postId, $metaKey, $metaValue);
}
Or this just to update the value on many keys:
// Or maybe you want to update a bunch of keys with the same value for some reason..
$metaValue = "10";
$metaKeys = Array("key1", "key2"); // as many keys as you want here
foreach($metaKeys as $metaKey) {
update_post_meta($postId, $metaKey, $metaValue);
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0