Support

Account

Home Forums Add-ons Options Page Update Custom Post Type ACF with Options ACF Reply To: Update Custom Post Type ACF with Options ACF

  • So I ended up figuring this out – the above code is (mostly) entirely wrong. For anyone else needing a bump in the right direction this is what I have currently working – though note that it is “unfinished” but the code structure is correct now, I just haven’t added all of the fields I am using.

    
    function update_fuel_prices() {
    
    	$screen = get_current_screen(); // make sure we're on the right options page
    	if( strpos($screen->id, 'acf-options-fuel-price-table') == true ) {
    
    		// setup args for existing locations (total of 11)
    		$args = array(
    			'post_type' => 'location',
    			'post_status' => 'publish',
    			'posts_per_page' => -1,
    			'orderby' => 'title',
    			'order' => 'ASC'
    		);
    		// $loc_id = wp_insert_post( $location );
    
    		$loc_query = new WP_Query( $args );
    
    		$loc_fuel_available_key = 'field_5809a9ba8e8f5'; // fuel_availability - Checkbox (checkbox options above)
    		$loc_unleaded_key = 'field_580095114ff69';       // unleaded_cash_price - Number
    		$loc_unleaded_88_key = 'field_5d314a346e78c'; 	 // unleaded_88_cash_price - Number
    		$loc_diesel_key = 'field_5809a9838e8f4'; 		 // diesel_cash_price - Number
    		$loc_bulk_key = 'field_5809aa1e8e8f6'; 			 // bulk_def_cash_price - Number
    		$loc_hide_price_key = 'field_5d314d5ace408'; 	 // hide_unleaded_cash_price - True/False
    
    		if( $loc_query->have_posts() ) :
    			while( $loc_query->have_posts() ) : $loc_query->the_post();
    
    				$post_id = get_the_ID();
    				$post_title = get_the_title();
    				//error_log($post_id . ' - ' . $post_title); // ensure we have proper location data
    
    				// Optional Check - make sure we have data to update with
    				if( have_rows('fuel_price_repeater', 'options') ) :
    
    					// get our repeater field values
    					$op_fuel_available_value = get_sub_field( 'fuel_availability_upd', 'options' ); // Checkbox
    					$op_unleaded_value = get_sub_field( 'unleaded_cash_price_upd', 'options' ); // Number
    					$op_unleaded_value = get_sub_field( 'unleaded_cash_price_upd', 'options' ); // Number
    					$op_unleaded_88_value = get_sub_field( 'unleaded_88_cash_price_upd', 'options' ); // Number
    					$op_diesel_value = get_sub_field( 'diesel_cash_price_upd', 'options' ); // Number
    					$op_bulk_value = get_sub_field( 'bulk_def_cash_price_upd', 'options' ); // Number
    					$op_hide_price_value = get_sub_field( 'hide_unleaded_cash_price_upd', 'options' ); // True/False
    
    					// update the location fields
    					update_field( $loc_unleaded_key, $op_unleaded_value, $post_id );
    				endif;
    
    			endwhile;
    		endif;
    
    	} // end of screen check
    }
    
    // when the update button is clicked - run this function
    add_action( 'acf/save_post', 'update_fuel_prices' );