Support

Account

Home Forums General Issues Getting the ID of the current post before wp_handle_upload_prefilter Reply To: Getting the ID of the current post before wp_handle_upload_prefilter

  • 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