简体   繁体   English

add_post_meta() WORDPRESS 中的 function 无法正常工作

[英]add_post_meta() function in WORDPRESS not working properly

One of the parameters of this function is the value for the meta_key.这个 function 的参数之一是 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.当我使用像“hello”或“house”这样的基本字符串作为值时,function 工作正常,但是当我将该值存储在变量中时,我不知道为什么不能正常工作,它只是没有将 de 值存储在自定义字段。 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.但是,您可能曾经使用过带有 static 值的 add_post_meta 并且第二次将其添加为变量,因此 add_post_meta 第二次将无法工作。 Instead you can use update_post_meta() to update the values.相反,您可以使用 update_post_meta() 来更新值。

update_post_meta() will update/add value of the existing meta key. update_post_meta() 将更新/添加现有元键的值。 If the value is not exists, use add_post_meta($post_ID, $meta_key, $meta_value).如果该值不存在,请使用 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 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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