0

I tried this on localhost and it worked, but then I tried to run this on live server (the same template, everything) and it doesn't work. Any help?

add_post_meta('39', 'overall', '4');
3
  • Is it necessary to put post_id as a string? Commented Jul 18, 2016 at 11:44
  • It doesn't work even withou string
    – user97964
    Commented Jul 18, 2016 at 11:47
  • 1
    it doesn't work is not a good statement for us in too distant to judge the situation. edit your question, don't comment, and provide us more details, otherwise it'll be voted close for too localized problem. Commented Jul 18, 2016 at 11:52

1 Answer 1

1

you have pass static postID which is not match at your live server. Please check the ID of post. For good practice pass the dynamic ID as per documentation.

<?php add_post_meta($post_id, $meta_key, $meta_value, $unique); ?> 

$post_id --> The ID of the post to which a custom field should be added. $meta_key --> The key of the custom field which should be added. $meta_value --> The value of the custom field which should be added. If an array is given, it will be serialized into a string. $unique --> Whether or not you want the key to stay unique. When set to true, the custom field will not be added if the given key already exists among custom fields of the specified post.

Or try this logic

<?php
if ( ! add_post_meta( 39, 'overall', 4, true ) ) { 
   update_post_meta( 39, 'overall', 4);
}
1
  • You can just use update_post_meta. If the key is not found, update_post_meta will call add_post_meta with the same parameters.
    – Gavin
    Commented Jul 8, 2017 at 20:23

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.