After many hours of trial and error and much frustration, I was able to figure this out. Here’s the code that worked for me to retrieve the correct ID so that I could use a variable in the rewritten file name:
function process_upload_prefilter() {
// WP Handle Upload Prefilter:
add_filter('wp_handle_upload_prefilter', 'rename_acf_upload', 11, 1);
}
function rename_acf_upload( $file ) {
$current_post_id = $_REQUEST['post_id']; // FINALLY RETRIEVED THE ID SUCCESSFULLY!!!
$company_name = get_field('company_name', $current_post_id);
$file['name'] = $company_name . '-C.pdf';
return $file;
}
// ACF Upload Prefilter: (report_link is the name of the field that I want to do the upload prefiltering on)
add_filter('acf/upload_prefilter/name=report_link', 'process_upload_prefilter' );
Hope it helps someone else save some time in the future.
Best,
Jack
Thanks for sharing your code.
I may be mistaken, but I believe that I can add a simplification to ituk’s post. If you just want the first item in the repeater, you can target it with the [0] array index without needing to reset the repeater field. So the following chunk of ituk’s code:
$repeater_field = $_POST['acf']['field_57bc841e7f973']; //repater parent key
reset($repeater_field); //reset repeater array to the rist row
$first_field = key($repeater_field); //get first key in the repeater array
$my_text_field = $_POST['acf']['field_57bc841e7f973'][$first_field]['field_57bc8a5447d06']; //specific field
can be simplified to this:
$my_text_field = $_POST['acf']['field_57bc841e7f973'][0]['field_57bc8a5447d06']; //specific field
NOTE: This only works for the 1st item! It seems that the first item uses the [0] array index but each repeater subfield that gets added gets a randomly generated array index. So instead of going to [1] for the second item it will be something like [58b641f67f3de], which is a randomly generated string that is probably generated from the server’s timestamp.
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.