Skip to content

Instantly share code, notes, and snippets.

@mishterk
Last active September 3, 2021 16:22
Show Gist options
  • Save mishterk/fab2cd441c873b1884a0e446d6c99fd7 to your computer and use it in GitHub Desktop.
Save mishterk/fab2cd441c873b1884a0e446d6c99fd7 to your computer and use it in GitHub Desktop.
How to store ACF field values for WordPress attachments in custom database tables.

Using ACF Custom Database Tables for attachment meta

The only thing stopping the use of ACF Custom Database Tables for attachment fields is the logic we currently have in place that limits the use of the UI depending on a field group's location rules. The plugin expects either a post type or a user location rule and if it can't find one, it won't allow a table to be enabled for the field group in question so, when an Attachment location rule is used, the creation of a DB table is blocked as there is no post type in the location rules array.

This can be worked around by simply adding the attachment post type to the Post Type location rule values list, as per the code snippet below.

You can then set up the location rules as follows:

ACF Location Rules

With this combination of location rules in place, the custom table UI will be available and when field values are entered in the media library, the custom DB table is updated.

<?php
add_filter( "acf/location/rule_values/type=post_type", function ( $values, $rule ) {
$values['attachment'] = 'Attachment';
return $values;
}, 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment