7

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

1
  • I didn't know about that way of doing it, but yes, it should work and your code looks correct. Maybe you want to post the rest of your code in case there's an error somewhere else. Perhaps $Post_ID is set wrong, or something else?
    – mozboz
    Commented Jul 29, 2020 at 0:30

2 Answers 2

7

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);
}
4
  • Hello, thanks for your answer. I have added a trial script which doesn't seem to work. Is it not doable with wp_update_post or am I missing on something within the script?
    – Ali Hamdan
    Commented Jul 28, 2020 at 21:09
  • 1
    meta_input with wp_update_post () does exactly the same thing that mozboz's code does. It just loops over each key and runs update_post_meta(). Commented Jul 29, 2020 at 0:14
  • Ah, I didn't know about meta_input, thanks for the correction. Answer updated
    – mozboz
    Commented Jul 29, 2020 at 0:31
  • yep, as Jacob says, this is, sadly, not anymore efficient - it calls wp_insert_post - developer.wordpress.org/reference/functions/wp_insert_post and there in source code you can see
    – jave.web
    Commented Jan 19, 2022 at 17:51
0

You can also serialize the array

$metaValues = array(
    'key1' => 'value1',
    'key2' => 'value2', 
    //   ... as many key/values as you want
);

update_post_meta($postId,'post_meta_name', serialize($metaValues));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.