How to prepend parent post ID to uploaded file
-
Hi,
I have a form using ACF fields where subscribers can create a custom post type (Paper) and an ACF upload field which subscribers can use to upload a PDF/Word document to a custom folder in the wp-content/uploads directory.
I need to add the ID of the parent (custom) post to the front of the file name, eg 435-this-is-my-file-name.pdf
I have managed to prepend a string ‘test’ which works, but I have no idea how to get the media item’s parent post ID. My current code is:
add_filter( 'wp_handle_upload_prefilter', 'custom_upload_filter' ); function custom_upload_filter($file) { $file['name'] = 'test-' . $file['name']; return $file; }
I’m guessing it’s along the following lines, but this doesn’t seem to work.
add_filter( 'wp_handle_upload_prefilter', 'custom_upload_filter' ); function custom_upload_filter($file) { //stuff to get parent post-id and prefix to file $parent_post_id if( isset($_REQUEST['post_id']) ) { $post_id = $_REQUEST['post_id']; } else { $post_id = false; } $file['name'] = $post_id'-' . $file['name']; return $file; }
Any pointers?
Cheers,
Tracy
- The topic ‘How to prepend parent post ID to uploaded file’ is closed to new replies.