Support

Account

Home Forums Backend Issues (wp-admin) Update Checkbox Field with Multiple Values using MySQL Queries

Solving

Update Checkbox Field with Multiple Values using MySQL Queries

  • Hi there!

    I’m using your instructions to build a simple $wpdb->query and avoid using 50-100 update_field() calls on a single page. (The page in question has a form with many inputs)

    Some of the fields in question are ACF Checkbox Fields that have multiple possible values (see the attached screenshot for more details).

    Using the below query works correctly (Note the ‘m5bull’ value being assigned on the trendline_20 checkbox field.)

     global $wpdb;
      $wpdb->query(
        "INSERT INTO wp_postmeta (post_id, meta_key, meta_value) 
          VALUES ($post_id, 'trendline_20', 'm5bull']),($post_id, '_trendline_20', 'field_634819cc60fd8')"
      );

    However, when attempting to replace ‘m5bull’ with an array for those cases when multiple values are selected, the query does not work. How do you insert an array in the place of ‘m5bull’? I’ve tried many variations with no luck.

    Thank you!
    Will

  • The array needs to be serialize

    
    $value_to_insert = serialize(array('value1', 'value2', 'value3', 'etc'));
    
  • Hi there! Thanks for your help with this.

    The below code works correctly:

    
    $trendline_20 = array($_POST['trendline_20-m5bull'], $_POST['trendline_20-m5bear'], $_POST['trendline_20-m15bull'], $_POST['trendline_20-m15bear'], $_POST['trendline_20-m30bull'], $_POST['trendline_20-m30bear'], $_POST['trendline_20-h1bull'], $_POST['trendline_20-h1bear'], $_POST['trendline_20-h2bull'], $_POST['trendline_20-h2bear'], $_POST['trendline_20-h4bull'], $_POST['trendline_20-h4bear'], $_POST['trendline_20-d1bull'], $_POST['trendline_20-d1bear']);
    
    update_field('trendline_20', $trendline_20, $post_id);
    

    However, when attempting to serialize and use $wpdb, the following does not work:

    
    $trendline_20 = serialize(array($_POST['trendline_20-m5bull'], $_POST['trendline_20-m5bear'], $_POST['trendline_20-m15bull'], $_POST['trendline_20-m15bear'], $_POST['trendline_20-m30bull'], $_POST['trendline_20-m30bear'], $_POST['trendline_20-h1bull'], $_POST['trendline_20-h1bear'], $_POST['trendline_20-h2bull'], $_POST['trendline_20-h2bear'], $_POST['trendline_20-h4bull'], $_POST['trendline_20-h4bear'], $_POST['trendline_20-d1bull'], $_POST['trendline_20-d1bear']));
    
    global $wpdb;
    
    $wpdb->query(
        "INSERT INTO wp_postmeta (post_id, meta_key, meta_value) 
          VALUES ($post_id, 'trendline_20', $trendline20),($post_id, '_trendline_20', 'field_634819cc60fd8')"
      );
    

    Why is $_POST working with update field() and not with $wpdb?

    Thanks again for your time and assistance.

  • I have no idea, it is causing an error? They value might need to be escaped in some way.

  • None that I can see! The form seems to submit, the page redirects, but the field value is null:

    
    if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "new_post" ) { 
      // Do some minor form validation to make sure there is content
      if (isset ($_POST['title'])) {
          $title =  $_POST['title'];
      } else {
          echo 'Please enter a  title';
      }
      if (isset ($_POST['description'])) {
          $description = $_POST['description'];
      } else {
          echo 'Please enter the content';
      }
      
      $trendline_20 = serialize(array($_POST['trendline_20-m5bull'], $_POST['trendline_20-m5bear'], $_POST['trendline_20-m15bull'], $_POST['trendline_20-m15bear'], $_POST['trendline_20-m30bull'], $_POST['trendline_20-m30bear'], $_POST['trendline_20-h1bull'], $_POST['trendline_20-h1bear'], $_POST['trendline_20-h2bull'], $_POST['trendline_20-h2bear'], $_POST['trendline_20-h4bull'], $_POST['trendline_20-h4bear'], $_POST['trendline_20-d1bull'], $_POST['trendline_20-d1bear']));
      $option_liquidity = $_POST['option_liquidity'];
      $option_liquidity_min = $_POST['option_liquidity_min'];
      $option_liquidity_max = $_POST['option_liquidity_max'];
       
      // Add the content of the form to $post as an array
      $new_post = array(
          'post_title'    => $title,
          'post_content'  => $description,
          'post_status'   => 'draft',           // Choose: publish, preview, future, draft, etc.
          'post_type' => 'custom_scan'  //'post',page' or use a custom post type if you want to
      );
      //save the new post
      $post_id = wp_insert_post($new_post);
    
      global $wpdb;
    
      $wpdb->query(
        "INSERT INTO wp_postmeta (post_id, meta_key, meta_value) 
          VALUES ($post_id, 'trendline_20', $trendline20),($post_id, '_trendline_20', 'field_634819cc60fd8')"
      );
    
      //redirect
      wp_redirect(get_permalink($post_id)); exit;
    } ?>
    
Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.