Support

Account

Home Forums Add-ons Repeater Field acf/input/form_data conflicting with DB query

Solved

acf/input/form_data conflicting with DB query

  • I have hit a bit of a wall.

    I have two post types, both with repeater fields. Let’s say Post_type_A looks like this:

    Repeater Field A
    -Subfield 1
    -subfield 2
    -Subfield 3
    -Unique ID

    And Post_Type_B looks like this:

    Repeater Field B
    -Select field (This is dynamically populated from Repeater field A, and returns the Unique ID as the $value)
    -Read Only Field 1 (Populated with Repeater_field_A_%_Subfield_1)
    -Read Only Field 2 (Populated with Repeater_field_A_%_Subfield_2)
    -Read Only Field 3 (Populated with Repeater_field_A_%_Subfield_3)
    -Subfield A
    -Subfield B (These are fields the user updates based on the information in the readonly fields)

    When a user creates a post for Post_Type_B, they select a post from Post_Type_A and the information from Repeater_Field_A is automatically imported into Repeater_Field_B. The select field is also set to the unique ID of each row. So, for example, if post A has five rows, the new post b will have five rows, one for each of post A’s rows. Plus, each row will be uniquely populated with the information from post A’s corresponding row.

    This works great to get the user started, but, if the user needs to add more rows to post b, they need to be able to select which row from post A they want it to pull data from. This is where I am hitting my wall. Here is my DB query:

    $rows = $wpdb->get_results($wpdb->prepare( 
    								"
    								SELECT * 
    								FROM {$wpdb->prefix}postmeta
    					            WHERE post_id = %s
    					            	AND meta_key LIKE %s
    					                AND meta_value = %s
    					            ",
    					            $audit_id,
    					            'sa_audit_items_%_sa_line_item_id', // meta_name: $ParentName_$RowNumber_$ChildName
    					            $audit_line_item_id // meta_value: 'type_3' for example
    					            
    							)); 
    							
    							add_action( 'acf/input/form_data', 'test_update_audit_line_items', 10, 1 );
    							
    							if( $rows ){
    								foreach( $rows as $row ){
    									preg_match('_([0-9]+)_', $row->meta_key, $matches);
    									$r = $matches[0]; 
    							
    								$audit_rows = get_field('sa_audit_items', $audit_id);
    								$current_audit_row = $audit_rows[$r];
    								
    								$audit_quantity = $current_audit_row['sa_existing_qty']; //Get Existing Quantity
    								$audit_existing_fixture = $current_audit_row['sa_existing_fixture']; //Get Existing Fixture Type - returns Post ID
    								$building = $current_audit_row['sa_building'];
    								$floor = $current_audit_row['sa_floor'];
    								$height = $current_audit_row['sa_height_ft'];
    								$voltage = 	$current_audit_row['sa_voltage'];
    								$existing_fixture_title = get_the_title($audit_existing_fixture);
    								$existing_qty = $audit_quantity = $current_audit_row['sa_existing_qty'];
    								$burnouts = $current_audit_row['sa_burn_qty'];
    								$annual_burn = $current_audit_row['sa_burn_hours'];
    								$loc_notes = $current_audit_row['sa_location_notes'];
    								$fix_note = $current_audit_row['sa_fixture_notes'];
    								
    								} ##End Foreach
    							} 	##End if

    This works perfectly and pulls all of the data I need it to when I run it in my page-template and just dump the variables.

    The problem I am having, is when I wrap it in a function and add the acf/input/form-data action it breaks the form. It just won’t show up. I haven’t been able to figure out why running a DB query inside of that action is causing a problem or how to get around it. Any suggestions would be greatly appreciated. Here is the full function and action call:

    function test_update_audit_line_items() {
    	$sq_row_count = count(get_field('sq_repeat_sol'));
    	if( $sq_row_count != 0 ) { 
    		$post_type = get_post_type();
    		if( $post_type == 'sales_quote' ) {
    			$status = get_field('sq_status'); 
    			if( $status == 'inprogress' ) { //Check to see that quote is still in progress 
    				
    				$audit_id = get_field('sq_import_audit');
    					if( have_rows('sq_repeat_sol') ) :
    						while( have_rows('sq_repeat_sol') ) : the_row();
    	
    							
    							//Update Audit Line item info if changed 
    							$audit_line_item_id = get_sub_field('sq_audit_line_item');
    							
    							remove_action( 'acf/input/form_data', 'test_update_audit_line_items', 10, 1 );	
    							//Query DB for matching ID
    							$rows = $wpdb->get_results($wpdb->prepare( 
    								"
    								SELECT * 
    								FROM {$wpdb->prefix}postmeta
    					            WHERE post_id = %s
    					            	AND meta_key LIKE %s
    					                AND meta_value = %s
    					            ",
    					            $audit_id,
    					            'sa_audit_items_%_sa_line_item_id', // meta_name: $ParentName_$RowNumber_$ChildName
    					            $audit_line_item_id // meta_value: 'type_3' for example
    					            
    							)); 
    							
    							add_action( 'acf/input/form_data', 'test_update_audit_line_items', 10, 1 );
    							
    							if( $rows ){
    								foreach( $rows as $row ){
    									preg_match('_([0-9]+)_', $row->meta_key, $matches);
    									$r = $matches[0]; 
    							
    								$audit_rows = get_field('sa_audit_items', $audit_id);
    								$current_audit_row = $audit_rows[$r];
    								
    								$audit_quantity = $current_audit_row['sa_existing_qty']; //Get Existing Quantity
    								$audit_existing_fixture = $current_audit_row['sa_existing_fixture']; //Get Existing Fixture Type - returns Post ID
    								$building = $current_audit_row['sa_building'];
    								$floor = $current_audit_row['sa_floor'];
    								$height = $current_audit_row['sa_height_ft'];
    								$voltage = 	$current_audit_row['sa_voltage'];
    								$existing_fixture_title = get_the_title($audit_existing_fixture);
    								$existing_qty = $audit_quantity = $current_audit_row['sa_existing_qty'];
    								$burnouts = $current_audit_row['sa_burn_qty'];
    								$annual_burn = $current_audit_row['sa_burn_hours'];
    								$loc_notes = $current_audit_row['sa_location_notes'];
    								$fix_note = $current_audit_row['sa_fixture_notes'];
    								
    								} ##End Foreach
    							} 	##End if
    								//Update the subfields
    								update_sub_field( 'field_5727c74cd3752', $building);
    								update_sub_field( 'field_5727c7bfd3753', $floor);
    								update_sub_field( 'field_5727c86dd3755', $voltage);
    								update_sub_field( 'field_5727c882d3756', $existing_fixture_title);
    								update_sub_field( 'field_5727c906d3757', $audit_quantity);
    								update_sub_field( 'field_5727c93fd3758', $burnouts);
    								update_sub_field( 'field_5727cd0026f69', $fix_note);
    								update_sub_field( 'field_5727ccbe26f68', $loc_notes);
    								update_sub_field( 'field_5727c906d3757', $audit_quantity);
    								
    							
    								
    						endwhile;
    					endif;	
    				}
    		}					
    	}
    }
    
    add_action( 'acf/input/form_data', 'test_update_audit_line_items', 10, 1 ); 
  • My suspicion is that there’s a PHP error in the function. For example, you are using $wpdb but to use this object inside a function you need to declare it global $wpdb;.

    Turn on error reporting, you’ll probably want to do it using a log rather than displaying them, put this in wp-config.php, https://codex.wordpress.org/WP_DEBUG

    
    define('WP_DEBUG', true);
    define('WP_DEBUG_DISPLAY', false);
    define('WP_DEBUG_LOG', true);
    
  • You’re Brilliant @hube2! This fixed it!

    Here is a simplified version of how the code worked:

    function update_sub_fields() {
    
      global $wpdb; //declare using a global variable
    
      $import_post_id = get_field('import_post'); //get the id of the post we are importing data from
    
      if( have_rows('repeater_field_name') ):  //start the repeater loop
         while( have_rows('repeater_field_name') ): the_row();
    
             $unique_id = get_sub_field('unique_id_field'); //get the unique id
    
             $rows = $wpdb->get_results($wpdb->prepare( 
    		"
    		SELECT * 
    		FROM {$wpdb->prefix}postmeta
    		WHERE post_id = %s
    		AND meta_key LIKE %s
    		AND meta_value = %s
    		",
    		$import_post_id, //only look at meta-values that match the post we are importing from
    		'import_repeater_field_%_unique_id_field', // meta_name: $ParentName_$RowNumber_$ChildName
    		$unique_id // meta_value: to match our unique id
    	)); 
    							
    							
    	if( $rows ){
    	   foreach( $rows as $row ){
    		preg_match('_([0-9]+)_', $row->meta_key, $matches);
    		$r = $matches[0]; 
    							
    	        $import_rows = get_field('import_repeater_field', $import_post_id);
    		$current_import_row = $import_rows[$r];
    
                    $sub_field_to_import = $current_import_row['sub_field_to_import'];
    
                    update_sub_field('new_field_to_update', $sub_field_to_import);
                    
                } //End foreach
              } //End if
            endwhile; //end loop
          endif; 
    }
    
    add_action( 'acf/imput/form_data', 'update_sub_fields', 10, 1);

    I hope this helps anyone else who might be looking to import data from one repeater field to another!

    Jess

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘acf/input/form_data conflicting with DB query’ is closed to new replies.