Nevermind just figured it out, I nested the forloops
Heres my code if it helps someone else, just trying to stop it from returning values that dont relate to the current post:
function acf_event_cost_options( $form ){
foreach ($form['fields'] as &$field) {
if(strpos($field->cssClass, 'acf_event_cost_options') === false)
continue;
//This only auto-populates a field which has the class dynamicfield. If it doesn't then, continue ! Don't forget to add a class to the field you want to dynamically populate in your form.
global $post;
$id = $post->ID; // Important - this is what tells the form to only use values from the individual post, not all posts
global $wpdb;
$rows_name = $wpdb->get_results($wpdb->prepare(
"
SELECT *
FROM wp_postmeta
WHERE post_id = %d AND meta_key LIKE %s
",
$id,
'event_cost_structure_%_option_name' //enter the actual name of your repeater field and sub-field names, the % is a variable that represents the row number
));
$rows_price = $wpdb->get_results($wpdb->prepare(
"
SELECT *
FROM wp_postmeta
WHERE post_id = %d AND meta_key LIKE %s
",
$id,
'event_cost_structure_%_option_price' //enter the actual name of your repeater field and sub-field names, the % is a variable that represents the row number
));
$choices = array(array('text' => 'Please Choose an Option', 'value' => ' ', 'price' => ' '));
//Default choice for dynamic select field. Value is blank, so if it's required it won't validate, change the text to what you want it to be, e.g. 'Please Choose:'
if( $rows_name || $rows_price ) {
foreach ($rows_name as $row_name) {
foreach ($rows_price as $row_price) {
//If there are results, for each result, find the 'repeater row number'
preg_match('_([0-9]+)_', $row_name->meta_key, $matches);
$meta_key = 'event_cost_structure_' . $matches[0] . '_option_name';
$value = get_post_meta($post->ID, $meta_key, true);
//Get the subfield value from the row.
//If there are results, for each result, find the 'repeater row number'
preg_match('_([0-9]+)_', $row_price->meta_key, $matches);
$meta_key_price = 'event_cost_structure_' . $matches[0] . '_option_price';
$value_price = get_post_meta($post->ID, $meta_key_price, true);
//Get the subfield value from the row.
$choices[] = array('text' => $value . ' $' . $value_price . '.00', 'value' => $value, 'price' => $value_price);
//Create an option array for each subfield value.
}
}
}
$field->choices = $choices;
//Once we've looped through all of the subfields values and created all of our options, we assign the $choices array (which has all of our new options) to the $field choices property.
}
return $form;
//Display the new dynamically populated form.
}
Sorry to resurrect an old thread, but this is exactly what I am looking to do and have modified to my needs, but can not for the life of me figure out how to add another table value into the equation?
In this example we are looking into the database for:
‘room_types_%_room_type’
in my example I need to also find another:
‘room_types_%_room_price’
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.