What is this plugin?
The ACF Custom Database Tables plugin is an extension for the Advanced Custom Fields plugin. The extension makes it possible for developers to generate custom database tables that map to ACF Field Groups where each field represents a column. This approach provides the option to store key entity (post, user, custom post type) metadata as a single row of normalized data.
- Table generation, creation, and modification from the WordPress admin
- Filters to prevent particular fields from generating a column
- Filters to disable core metadata storage where a custom table is available for that data
- Filters to enable join tables for eligible ACF field types
- Filters to remove support for particular fields
Why would I use it?
The plugin offers a few key benefits:
- Organised, structured data is more readily available for export and can be searched efficiently.
- By moving data out of the WordPress core meta tables, these tables can be more efficient in their own operations.
- Increased efficiency in database operations frees up system resources increasing scalability of your site/application.
- Advanced use offers join tables for eligible fields opening up the possibility of relationship-oriented custom SQL queries.
How does it work?
The basic process is as follows:
- The plugin introduces an option on an ACF Field Group’s admin page the allows a site administrator to enable a custom database table for that field group.
- On Field Group save, a JSON file is generated – this is the table definition.
- On a subsequent, manually-triggered action, these JSON files are converted to SQL and are then used to create new database tables or modify existing ones. A table map is also generated as part of this process.
- The plugin uses the generated map to intercept metadata as it is saved and retrieved from the database. If that piece of metadata has a custom table defined for the related object (post, user, custom post type), the data will be stored/retrieved to/from that custom table.
The basic requirements for the plugin to function are as follows:
- PHP version 7.0
- WordPress version 5.7
- ACF version 5.10
Note: the plugin may work on older versions but we do develop for modern versions so it is best to remain updated at all times.
ACF Local JSON
Whilst not a requirement, we highly recommend using ACF JSON. Doing so enables you to further reduce the amount of metadata in your core meta tables by disabling ACF field key reference storage.
Enabling ACF JSON is as simple as creating an acf-json directory inside your theme directory. See https://www.advancedcustomfields.com/resources/local-json/ for further information.
If you decide to bypass both values and field key references you must have ACF JSON enabled and have all field groups saved in JSON format in order for ACF to work correctly. Without field key references in the database, ACF relies on the ACF JSON field groups to determine field mappings. Without those, data will not be stored correctly.
Supported field types
The plugin currently supports all built-in field types except for the following:
- Flexible Content
The above fields are complex and support for these field types will ship in a future 1.x release.
Where a field type is unsupported, it will be ignored by the plugin and will be passed back to ACF to be handled as per usual (stored in core metatables).
Layout fields such as message, accordion, and tab are not considered as they do not store data.
Custom Field Types
There is some basic support in place for custom field types, but these field types need to be enabled/registered through the use of a simple WordPress filter. See activating support for a custom field type for instructions on how to do this.