Fetch details in wordpress

I have inserted values product-price, name and size in wordpress. The insert code is below

function products_save_postdata($post_id) {
  if (isset ($_POST['price'])) {
    update_post_meta($post_id, 'price', esc_attr($_POST['price']));
  }
  if (isset ($_POST['product_name'])) {
    update_post_meta($post_id, 'product_name', esc_attr($_POST['product_name']));
  }
  if (isset ($_POST['size'])) {
    update_post_meta($post_id, 'size', esc_attr($_POST['size']));
  }
}

How can i fetch this inserted data?

Related posts

Leave a Reply

1 comment

  1. You can use this function instead of yours:

    function products_save_postdata($post_id)
    {
      $array = array();
    
      if (isset ($_POST['price'])) {
        update_post_meta($post_id, 'price', esc_attr($_POST['price']));
        $array['price'] = $_POST['price'];
      }
      if (isset ($_POST['product_name'])) {
        update_post_meta($post_id, 'product_name', esc_attr($_POST['product_name']));
        $array['product_name'] = $_POST['product_name'];
      }
      if (isset ($_POST['size'])) {
        update_post_meta($post_id, 'size', esc_attr($_POST['size']));
        $array['size'] = $_POST['size'];
      }
      return $array;
    }
    

    Get values with:

    $post_data = products_save_postdata($post_id);
    
    echo $post_data['price'];
    echo $post_data['product_name'];
    echo $post_data['size'];