0

The below code is added into a page template file, when someone loads the page it will add the post meta if it didn't exist, it will update the post meta if it did exist. When I get the post meta and then return it, the outcome is "Array".

It leads me to believe the post meta is not being made or the get isn't working.

global $post;
if ( ! add_post_meta( $post->ID, 'offer_voucher_evergreen_deadline', '2018-11-16 13:00:00')) { 
    update_post_meta($post->ID, 'offer_voucher_evergreen_deadline', '2018-11-16 13:00:00');
}

$test = get_post_meta($post->ID, 'offer_voucher_evergreen_deadline', true);
echo $test;
Daniel Vickers
  • 1,054
  • 1
  • 12
  • 32

1 Answers1

0

Try This:

global $post;
$meta_value = get_post_meta($post->ID, 'offer_voucher_evergreen_deadline', true);
if( !empty($meta_value) ) {
   update_post_meta($post->ID, 'offer_voucher_evergreen_deadline', '2018-11-16 13:00:00');
}else {
   add_post_meta( $post->ID, 'offer_voucher_evergreen_deadline', '2018-11-16 13:00:00')
}

$test = get_post_meta($post->ID, 'offer_voucher_evergreen_deadline', true);
echo $test; //return $test;

Note: Use a 3'rd param in get_post_meta() function, which will return a single value

$test = get_post_meta($post->ID, 'offer_voucher_evergreen_deadline', true);
echo $test; //return $test;

See get_post_meta() function for more details.

  • Saw this before you answered, adding true displays nothing so it must be the issue with adding the post meta. – Daniel Vickers Oct 17 '18 at 20:59
  • Never mind I found where it was echo'ing it, although I found it just before your answer I will mark it as correct. – Daniel Vickers Oct 17 '18 at 21:01
  • ok, great to know that you figure it out. you can share your fix. Also, I have updated the answer. – Mahfuzul Hasan Oct 17 '18 at 21:15
  • I made a new post for another issue relating to this, feel free to check it out https://stackoverflow.com/questions/52863861/wordpress-on-edit-post-page-save-update-post-meta – Daniel Vickers Oct 17 '18 at 21:32