Home › Forums › Add-ons › Options Page › Update Custom Post Type ACF with Options ACF
I have created an Options Page that lives as a sub-page under the Custom Post Type I am trying to update. The CPT is tied to a lot of critical data – so instead of rewriting a large portion of the site, I figured I would just use an Options page to update the CPT ACF that way the rest of the code can stay as-is.
I have the Options Page working and setup. I’m also able to save values to the Options Page, but they do not update the CPT ACF. I have also tried manually saving the CPT, but the values never update there either.
I also have a post object that I intended to use as an identifier to check which post object was selected and compare it to the location currently being looped – but I could not get that working either.
The fields of the CPT and Options are identical except the Options page ACF lives in a repeater (1 row for each CPT): the only difference being I have appended _upd
to the end of the Options field names to differentiate them. The Data I am trying to update is also in a Tab if that matters.
I’m not sure if i’m using the wrong hook or going about this entirely wrong. It seemed simple enough at first..
This is what I have so far in my functions.php
file:
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 ) {
/** pseudo code layout of implementation
foreach repeater as item
get repeater values
foreach location cpt
update fields with repeater values above
endforeach
endforeach
Potentially use 2 separate foreach loops and save repeater data as an array to loop through inside locations loop
auto-save / publish the data in the CPT without having to manually save each post
maybe use update_post_meta() instead - with ACF?
**/
// setup args for existing locations (total of 11)
$location = array(
'post_type' => 'location',
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC'
);
// $loc_id = wp_insert_post( $location );
// Get our location's acf field keys (fields that will be updated)
// Current checkbox options - "regular", "regular_unleaded", "unleaded_88", "mid_grade", "premium", "diesel", "bulk_def"
$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
// Get our Option's acf field keys (fields used to update the above fields)
$repeater_key = 'field_5da530ac6102d'; // fuel_price_repeater - Repeater
$choose_location = 'field_5da539726102e'; // choose_location - Post Object (single select only)
$op_fuel_available_key = 'field_5da52107683c7'; // fuel_availability_upd - Checkbox
$op_unleaded_key = 'field_5da521a4ad7d7'; // unleaded_cash_price_upd - Number
$op_unleaded_88_key = 'field_5da521e5ad7d8'; // unleaded_88_cash_price_upd - Number
$op_diesel_key = 'field_5da524e1ad7d9'; // diesel_cash_price_upd - Number
$op_bulk_key = 'field_5da52550ad7da'; // bulk_def_cash_price_upd - Number
$op_hide_price_key = 'field_5da5256dad7db'; // hide_unleaded_cash_price_upd - True/False
$repeater = get_field( 'fuel_price_repeater', 'options' );
// start loop for each repeater on Fuel Options Page
foreach( $repeater as $item ) :
$post_objects = get_sub_field( 'choose_location', 'options' );
$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_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
// if( $post_objects ) :
// $temp_post = $post;
foreach( $location as $loc ) :
$lid = $loc->ID;
//if( $temp_post->title == $loc->title ) {
update_field( $loc_fuel_available_key, $op_fuel_available_value, $lid );
update_field( $loc_unleaded_key, $op_unleaded_88_value, $lid );
update_field( $loc_unleaded_88_key, $op_unleaded_88_value, $lid );
update_field( $loc_diesel_key, $op_diesel_value, $lid );
update_field( $loc_bulk_key, $op_bulk_value, $lid );
update_field( $loc_hide_price_key, $op_hide_price_value, $lid );
//}
endforeach;
// endif;
endforeach;
} // end of screen check
}
// when the update button is clicked - run this function
add_action( 'acf/save_post', 'update_fuel_prices', 10, 3 );
This is how I created the options page:
if( function_exists('acf_add_options_page') ) :
acf_add_options_sub_page( array(
'page_title' => 'Fuel Prices',
'menu_title' => 'Fuel Price Table',
'parent_slug' => 'edit.php?post_type=location',
'capability' => 'edit_posts',
'redirect' => false
));
endif;
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' );
I couldn’t edit the above post so I’ll put this here. There is a mistake on the second “if” where it checks the repeater for rows.
It should be: if( have_rows('fuel_price_repeater', 'options') )
: the_row();
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.