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');
  • Is it necessary to put post_id as a string? – Mayeenul Islam Jul 18 '16 at 11:44
  • It doesn't work even withou string – STLLkv Jul 18 '16 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. – Mayeenul Islam Jul 18 '16 at 11:52

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

if ( ! add_post_meta( 39, 'overall', 4, true ) ) { 
   update_post_meta( 39, 'overall', 4);
  • 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 Jul 8 '17 at 20:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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