admin管理员组

文章数量:1287778

I am trying to add post-meta value and after this update query and after this get_post_meta value . but add_post_meta does not work .

<?php add_post_meta($post_id, 'Product_Year', trim( $_POST['Product_Year'])); ?>
  <?php update_post_meta($post_id, 'Product_Year', trim( $_POST['Product_Year'])); ?>

echo $year=get_post_meta($post_id , 'Product_Year',true );

I am trying to add post-meta value and after this update query and after this get_post_meta value . but add_post_meta does not work .

<?php add_post_meta($post_id, 'Product_Year', trim( $_POST['Product_Year'])); ?>
  <?php update_post_meta($post_id, 'Product_Year', trim( $_POST['Product_Year'])); ?>

echo $year=get_post_meta($post_id , 'Product_Year',true );
Share Improve this question asked Aug 2, 2017 at 6:19 sanjaysanjay 1035 bronze badges 1
  • you can value GET or not in $post_id? – Jignesh Patel Commented Aug 2, 2017 at 8:57
Add a comment  | 

2 Answers 2

Reset to default 1

You dont need to use add_post_meta and update_post_meta for same meta_key.

You can simply use update_post_meta function and this will add if the meta_key does not exist and will update if meta_key exists.

Add last parameter, refer this https://codex.wordpress/Function_Reference/add_post_meta

add_post_meta($post_id, 'Product_Year', trim( $_POST['Product_Year']), true);

本文标签: custom fieldadd post meta value