The related Gist can be found here: https://gist.github.com/mishterk/fab2cd441c873b1884a0e446d6c99fd7
Video Transcript
So I received a very interesting question this morning from customer, and they'll Basically ask him whether or not it would be possible to use ACF custom database tables to store attachment metadata in custom tables. Now, obviously ACF allows you to specify location rules that allow you to display ACF fields on the attachment form. Right. And I've got thinking about it and I thought, well, that should be possible because attachments are a post type. So if you can select the attachment post type, you, you, there should be no issue here. So I thought what I'd do is I'd fire up a test and see how this works, because this isn't a part of ACF. Well, this isn't a feature of ACF that I use very often. In fact, I don't think I've ever used this on a project myself, but basically what I needed to do was fire up a new field group and see what was possible here. So I set up this attachment data field group with a couple of fields in there. And as soon as I went to set up the location rules, I realised this is where you'd like it, you would just become stuck trying to use custom database tables. Okay. So, and the reason being is that the plugin expects either a post type or a user form of some description, right? So if you have a post type and you can select a post time there, you can enable a table, or if you have a user of some, some type or use a form of subscription, you can enable the table and the, with attachments in ICF. So the attachment post type doesn't appear in the post types list. Okay. So that's a little bit that's like the first kind of roadblock. And then when you go and you select attachment the custom database tables, plugin, it, it reviews these location rules and says, Hey, you can't create a table because, you know, like I can't create a table for that object. And so yeah, I Had to do a little bit of lateral thinking here on, on how I would get this working. And basically it's, it's not that hard, like as possible. So the trick to doing this is to actually set up a custom location rule. Well, no, not to set up custom location rule. It's actually just to add a value to the post type location rules, possible values that allow you to select the attachment post type. So what I've done is I've set up a chest and I'll link to this and the the description. So you can find it and read a little bit about it, but I've created this just where it's got a code snippet and it's really simple code snippet. It's basically just a filter that hooks into ACS. What hooks into ACF into the location rule values, it's ACF slash locations slash rule values. And we specify the type as post type because that's the location rule that we want to effect. And we're just going to add the attachment post type to the list of selectable values, sort of save that. And I come back over to our admin reload this. So if I change this over to post type and I can now select attachment, right. Now out of the box, it's probably not actually going to work cause ACF is going to use I mean, this will allow you to create the table, but these fields probably aren't going to appear, like if I go update this for a second, let's just have a look, okay. Back to media, I'll open one of these and you'll say dicey fields. They don't appear. Right. Because ACS expecting the, the location rules that it knows in order to render fields here. So basically what you do is you leave this rule in place. It says post type is able to attachment, and then you add another rule that specifies the attachment. I'll leave it at all. So it specifies the attachment form. Okay. And if I update that again And reload The media page or the media library. Okay. We'll see with our fields now here. Right? Okay. So the fields are in place. Now this isn't the whole, the entire puzzle though. So basically what we now need to do is create a table just to test this works, right? So I'm going to enable the table for this field group. I'm going to call it attach data. Okay. And hit update. Now we can see that a definition Jason file has been created. So this is the Jason used to create the table schemer and to rebuild the internal mapping system. So what I'll do is I'll hit manage database tables. Now go through this process. There's a big list of table files in here only because this is my development installation, so that a lot of stuff active on there, but I'll hit create and update tables. So this runs all the updates and changes to the database. And if I come and have a look at the database, now I reload here and we'll see that we've got our attachment data table and that's good, our field names in there. Okay. So this is looking good so far. And now when I come back to the media library, I'll just reload. And I'll start putting in values here. So let's go Taj Mahal. And with that reload, come over, check our database table. And we see that we're getting a field values and our custom table. And just to make sure that is working, let's put some more values here and come over to our table and reload. And there we go. We've got our second media items AC a field days in there. So it was a little interesting experiment and it just took a little bit of a lateral thinking, but it is possible to use ACF custom database tables on attachments. This is something that I will look at making possible without a snippet in a future release. But for now, like it's pretty simple snippet. And like I said, I'll link to the, just with the resources in the description and I'll throw a post on the hook town blog which hopefully helps people that are in the same situation.