I have been adding custom fields to my site using ACF. I see that for most field types, the “Field Name” setting corresponds to the meta_key in my db.
When I added a field with “Field Type” of files using the “Field Name” corresponding to the meta_key which I am using for the the file-path, I noticed that the post-edit -form did not pre-populate with what I have in the database, like the other field types. Furthermore, I noticed that if I add a new file, it gets added as a child post type, not a meta field (I also see this attested in the docs https://www.advancedcustomfields.com/resources/adding-fields-media-attachments/), and I didn’t see my chosen “Field Name” used at all. Even this data structure did not pre-populate on the post edit page.
I would like to 1. Be able to specify (in code or using the field group editor) the location where the file should be saved, 2. Have the form pull from and save to a meta field with the file location to be consistent with my current data model.
My question is: is this something I can do with ACF? I have been looking at the Action Hooks documentation and haven’t found any obvious candidates. I am thinking about going with something more custom than ACF, (or revising my existing data model), but wanted to check with the community and see if any of you have done anything like this before or if there was some canonical ACF way of doing it.
I’m not exactly sure by reading your description what you are trying to do.
When you add a file field there is a meta_key that corresponds to the field. When you upload a file this file is saved as a “post” of the post type “attachment” which is how WP stores all uploaded files. The value of the ACF field corresponds with the ID value of the attachment post that is created for the file.
And ACF file field cannot hold a URL and would not no what to do with a URL in this field, so it ignores the value that is in the DB if it is a URL.
Does that help at all? Can you give a better explanation of what you are trying to do?
Thanks John Huebner,
Here is my attempt at a better explanation. The part that is different for me is “When you upload a file this file is saved as a “post” of the post type “attachment” which is how WP stores all uploaded files.” I am working on a site which is being moved to wordpress from a non-wordpress site. As a result the attachment file structure is not standard for wordpress. The way they exist right now is 1. Each attachment exists in a custom attachment directory 2. Each attachment has a wp_postmeta entry which associates the file-path of the attachment to a post. It sounds like you are advising that I change my data model and file structure to what ACF expects (which is also “how wordpress does it”?). What I want is to use the plugin with my current data model, but if there are significant advantages to changing my data model, I am open to it. So far, ACF is the only thing that doesn’t work with my current data model, but if there will be other problems down the road, I might consider changing it. That attempt at a better explanation may have been a bit disorganized, I can try again if needed
If you want to use a URL then I would use a URL field, or potentially a text field so that I can allow relative URLS if that what you’re looking for.
But this will not work if you want the person to be able to upload a file because ACF uses the WP media uploader for files.
There are ways in WP, filters, that would allow you to specify a custom path where a file should be saved but there really isn’t any way to get around the way the WP handles files as “posts” using ACF.
You must be logged in to reply to this topic.
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!
© 2022 Advanced Custom Fields.