title: Update / Save Post Meta - PHP - Wordpress
body: <p>I'm using a plugin on Woocommerce that adds a new metabox with editable quantity number fields onto the admin back-end. 
  My theme allows for front-end product posting and so I want to add bring some of these fields to the front-end.</p>
  
  <p>I have managed to add the fields in my template file by using :</p>
  
  <pre><code>&lt;?php 
  $min   = get_post_meta( $post-&gt;ID, '_wpbo_minimum',  true );
  $max   = get_post_meta( $post-&gt;ID, '_wpbo_maximum',  true );
  ?&gt;
  
  &lt;label for="_wpbo_minimum"&gt;Minimum Quantity&lt;/label&gt;
  &lt;input type="number" name="_wpbo_minimum" value="&lt;?php echo $min; ?&gt;" /&gt;
  
  &lt;label for="_wpbo_maximum"&gt;Maximum Quantity&lt;/label&gt;
  &lt;input type="number" name="_wpbo_maximum" value="&lt;?php echo $max; ?&gt;" /&gt;
  </code></pre>
  
  <p>Both fields are showing on my front-end product edit form and getting their values from the backend if these were previously filled in.</p>
  
  <p>But now I am struggling with updating and saving the fields with updated values
  from front-end or saving new values from front-end if these values were not previously filled in.</p>
  
  <p>I spent hours finding some tutorial on this, I think I should be concentrating on the following code : </p>
  
  <pre><code>update_post_meta($post-&gt;ID, '_wpbo_minimum', $_POST['_wpbo_minimum']);
  update_post_meta($post-&gt;ID, '_wpbo_maximum', $_POST['_wpbo_maximum']);
  </code></pre>
  
  <p>But I can't figure out if this is correct and where should this code be placed in order to save or update my fields.</p>
  
  <p>I tried placing a function into my functions.php file that looks like this :</p>
  
  <pre><code>/* Update Minimum Qty field */ 
  add_action('save_post', 'save_min_qty');
  
  function save_min_qty($post_id)
  {
          if(get_post_type($post_id) != "VA_LISTING_PTYPE")
          return;
      $min   = get_post_meta( $post-&gt;ID, '_wpbo_minimum',  true );
      if ( isset( $_POST['_wpbo_minimum'] )) {
                  $min  = $_POST['_wpbo_minimum'];
              }
  
              if( isset( $_POST['_wpbo_minimum'] )) {
                  if ( $min != 0 ) {
                      $min = wpbo_validate_number( $min );
                  }
                  update_post_meta( 
                      $post_id, 
                      '_wpbo_minimum', 
                      strip_tags( $min )
                  );
  }
  </code></pre>
  
  <p>But this just doesn't seem to be the winner.</p>
  
  <p>Would you be please able to point me to the right direction ?</p>
  
  <p>Below is the code from the actual original plugin that creates metabox in the back-end, I am not sure if it's any relevant to what I need to do in front-end and what part of this and where should I be using it :</p>
  
  <pre><code>&lt;?php
  if ( ! class_exists( 'IPQ_Quantity_Meta_Boxes' ) ) :
  class IPQ_Quantity_Meta_Boxes {
      public function __construct() {
          add_action( 'save_post', array( $this, 'save_quantity_meta_data' ) );
      }
  
  
      /* Handle Saving Meta Box Data */   
      public function save_quantity_meta_data( $post_id ) {
  
          // Validate Post Type
          if ( ! isset( $_POST['post_type'] ) or $_POST['post_type'] !== 'product' ) {
              return;
          }
  
          // Validate User
          if ( !current_user_can( 'edit_post', $post_id ) ) {
              return;
          }
  
          // Verify Nonce
          if ( ! isset( $_POST["_wpbo_product_rule_nonce"] ) or ! wp_verify_nonce( $_POST["_wpbo_product_rule_nonce"], plugin_basename( __FILE__ ) ) ) {
              return;
          }
  
          // Update Rule Meta Values
          if ( isset( $_POST['_wpbo_minimum'] )) {
              $min  = $_POST['_wpbo_minimum'];
          }
  
          if( isset( $_POST['_wpbo_minimum'] )) {
              if ( $min != 0 ) {
                  $min = wpbo_validate_number( $min );
              }
              update_post_meta( 
                  $post_id, 
                  '_wpbo_minimum', 
                  strip_tags( $min )
              );
          }
  
          /* Make sure Max &gt; Min */
          if( isset( $_POST['_wpbo_maximum'] )) {
              $max = $_POST['_wpbo_maximum'];
              if ( isset( $min ) and $max &lt; $min and $max != 0 ) {
                  $max = $min;
              }
  
              update_post_meta( 
                  $post_id, 
                  '_wpbo_maximum', 
                  strip_tags( wpbo_validate_number( $max ) )
              );
          }       
      }
  }
  endif;
  </code></pre>