Does add_post_meta add or replace values in WordPress posts?

In a WordPress blog, I want to use in each post add_post_meta like in the following example:

add_post_meta( 7, 'fruit', 'banana', true )

If I want to set a different fruit for this post later, what is it going to happen? I mean the value will be replaced or added?

Read More

I just want to be sure it is always overwritten and replaced with the new one (always 1 value), NOT added. Obiously each post will have its value.

Related posts

1 comment

  1. You can use update_post_meta (you don’t need the true parameter in this case). The function will create the meta if it does not exist or will update it if it does.

    Codex: https://codex.wordpress.org/Function_Reference/update_post_meta

    To answer your original question add_post_meta will add multiple fields with the same key if the true parameter is not used, otherwise no changes will be made.

Comments are closed.