简体   繁体   中英

add_post_meta() function in WORDPRESS not working properly

One of the parameters of this function is the value for the meta_key. When I use a basic string like "hello" or "house" as a value the function works fine but when i store that value in a variable i don't know why don't work properly, it just not store de value in the custom field. Any help please thanks

// This work
add_post_meta( $post_ID, 'Name', 'Jack', true );

// This isn't
$name = "Jack";
add_post_meta( $post_ID, 'Name', $name, true );

It should works fine normally. However, it might happen that you have used add_post_meta once with static value and second time you add this as an variable so, add_post_meta won't work second time. Instead you can use update_post_meta() to update the values.

update_post_meta() will update/add value of the existing meta key. If the value is not exists, use add_post_meta($post_ID, $meta_key, $meta_value).

So you can use below code for adding/updating the post meta:

$name = "Jack";
update_post_meta( $post_ID, 'Name', $name, true );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM