Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Update an ACF custom field in wordpress with all the dealer id's when a select all
checbox is ticked.
Created by on 24th of July 2017
The ACF plugin is great but its not possible to "select all" records in a multi select
so we need to find a way to select all records so the user does not have to select each
record one by one. So easiest thing to do is make an extra checkbox field called "select all",
give it a value of "Yes", so when the post is updated, we grab all the id's we need to include,
update the custom field and reset the select all field to null so its not selected when the user goes
back to the editing page.
function white_label_update_excludes_list_select_all($post_id) {
// If this is a revision, get real post ID
if ( $parent_id = wp_is_post_revision( $post_id ) )
$post_id = $parent_id;
// Check if this post which has enabled the select all dealer function.
if ( $_POST['acf']['field_597612de3de1d'][0] == "Yes" ) {
// unhook this function so it doesn't loop infinitely
remove_action( 'save_post', 'white_label_update_excludes_list_select_all' );
// get a full list of all the current dealer_id's from our json feed
$dealer_site_data = file_get_contents(realpath($_SERVER["DOCUMENT_ROOT"]) . "/wp-content/plugins/".WHITE_LABEL_PLUGIN_FOLDER_NAME."/dealer-sites.json");
$dealer_site_data = json_decode($dealer_site_data,TRUE);
$count = 0;
foreach($dealer_site_data as $x) {
$dealer_id = $dealer_site_data[$count]["dealer_id"];
$array_of_dealer_ids[] = $dealer_id;
$count = $count + 1;
// update the post meta field: dealers_exclude_page_from_dealer_site
update_post_meta($post_id, 'dealers_exclude_page_from_dealer_site', $array_of_dealer_ids);
// reset the post meta field: dealers_exclude_page_select_all_dealers
update_post_meta($post_id, 'dealers_exclude_page_select_all_dealers', "");
// re-hook this function to avoid looping
add_action( 'save_post', 'white_label_update_excludes_list_select_all' );
add_action( 'save_post', 'white_label_update_excludes_list_select_all' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment