Support

Account

Home Forums General Issues How to update a field in a query

Helping

How to update a field in a query

  • Hi,

    I’m not a PHP expert, but managed to do the task up to a level. I need to update a radio field for the specific post only when the condition meets (purchased and used values gets equal) as per the below code. Instead this updates all the posts. Need help to solve this.

    <?php 
    
    function expired() {
      echo "Expired";
    }
    
    function rto_deactivation() {
      $post_id = false;
      update_field('field_59ba2159ddd3b', 'Deactive', $post_id);
    }
    
    // Issue on function rtoactivation(), when calls all RTOs get deactivated
    
    $i=1;
    $args = array(
      'numberposts' => -1,
      'post_type'   => 'rto_providers',
      'meta_key'    => 'package',
      'meta_value'  => 'Casual RTO'
    );
    
    $the_query = new WP_Query( $args );
    
    ?>
    <?php if( $the_query->have_posts() ): ?>
    
      <div class="table-responsive">
      <table class="table">
        <thead>
          <tr>
            <th>#</th>
            <th>RTO Name</th>
            <th>Email Address</th>
            <th>Purchased</th>
            <th>Used</th>
            <th>Status</th>
          </tr>
        </thead>
        <tbody>
    
      <?php 
      while( $the_query->have_posts() ) : $the_query->the_post(); 
      $email_id = get_field( 'email_address', $post_id );
      $number_of_enquiries = get_field( 'number_of_enquiries', $post_id );
      $account_activation = get_field( 'account_activation', $post_id );
      ?>
        <tr>
          <td><?php echo $i++; ?></td>
          <td><?php the_title(); ?></td>
          <td><?php echo $email_id; ?></td>
          <td><?php echo $number_of_enquiries; ?></td>
    
          <?php
          $used = $wpdb->get_var(" SELECT COUNT(*) FROM table_name WHERE form_name = 'Course Enquiry' AND field_value = '$email_id' ");
          ?>
    
          <td><?php echo $used; ?></td>
          <td class="<?php if ($number_of_enquiries == $used) echo 'red'; ?>"><?php if ($number_of_enquiries == $used) expired(); rto_deactivation(); ?></td>
        </tr>
      <?php endwhile; ?>
      </tbody>
      </table>
      </div>
    <?php endif; ?>
    
    <?php wp_reset_query(); ?>
  • Curious.. when you are using get_field and $post_id is that working as expected? Might need to change $post_id to get_the_id()??

    At any rate…
    Can you make these changes for testing, and let me know how it goes?

    
    function rto_deactivation($id) {
      update_field('field_59ba2159ddd3b', 'Deactive', $id);
    }
    
    // then call it like:
    
    rto_deactivation( get_the_id() );
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.