We recently had a request as to how you could prevent ACF Custom Database Tables from querying custom table data when using ACF’s get_field() function.
This isn’t something we had built-in but for anyone needing to implement this, you can use the following code to achieve exactly that.
A few things to keep in mind:
- This will ONLY work with versions 1.0.x as our service container and core object methods are changing in version 1.1.
- This approach calls directly upon our service container which is something that you shouldn’t get into the habit of doing unless you:
- Really know what you are doing and;
- Thoroughly test plugin updates in a non-production environment before releasing them. This is particularly important because our service container is not an API and is subject to change as the plugin evolves.
| <?php | |
| use ACFCustomDatabaseTables\Intercept\ACFGetFieldIntercept; | |
| use ACFCustomDatabaseTables\Vendor\Pimple\Container; | |
| /** | |
| * Class ACFCDTvOneDotZeroDotAnyGetFieldInterceptBypass | |
| * | |
| * This provides a 'hotfixed' approach for disabling custom database table intercept when using ACF's get_field() | |
| * function. This will only work with version 1.0.x versions of the plugin as a built-in tool will be available in | |
| * version 1.1 and beyond. | |
| */ | |
| class ACFCDTvOneDotZeroDotAnyGetFieldInterceptBypass { | |
| /** | |
| * Disables the get field intercept allowing data retrieval from core meta tables only. | |
| */ | |
| public static function enable() { | |
| if ( $instance = self::get_instance() ) { | |
| remove_filter( 'acf/pre_load_value', [ $instance, 'fetch_value' ], 10 ); | |
| } | |
| } | |
| /** | |
| * Enables the get field intercept allowing data retrieval from custom database tables. | |
| */ | |
| public static function disable() { | |
| if ( $instance = self::get_instance() and ! has_filter( 'acf/pre_load_value', [ $instance, 'fetch_value' ] ) ) { | |
| add_filter( 'acf/pre_load_value', [ $instance, 'fetch_value' ], 10, 3 ); | |
| } | |
| } | |
| /** | |
| * Fetches the get field intercept instance, if available. | |
| * | |
| * @return ACFGetFieldIntercept|null | |
| */ | |
| private static function get_instance() { | |
| $plugin = acf_custom_database_tables(); | |
| if ( ! method_exists( $plugin, 'container' ) ) { | |
| return null; | |
| } | |
| $container = $plugin->container(); | |
| if ( ! $container instanceof Container ) { | |
| return null; | |
| } | |
| $intercept_instance = $container['acf_get_field_intercept']; | |
| if ( ! $intercept_instance instanceof ACFGetFieldIntercept ) { | |
| return null; | |
| } | |
| return $intercept_instance; | |
| } | |
| } |
| <?php | |
| require 'ACFCDTvOneDotZeroDotAnyGetFieldInterceptBypass.php'; | |
| // EXAMPLE 1: | |
| // To disable get field intercepts entirely – which means all data is retreived from core meta | |
| // tables only – you could simply call the following from your functions.php file or a | |
| // configuration plugin: | |
| ACFCDTvOneDotZeroDotAnyGetFieldInterceptBypass::enable(); | |
| // EXAMPLE 2: | |
| // IF, however, you are in a template and only want to bypass certain fields as you call them, | |
| // you can turn the bypass on and off as follows: | |
| the_field('some_field'); // custom table will be queried | |
| ACFCDTvOneDotZeroDotAnyGetFieldInterceptBypass::enable(); | |
| the_field('some_other_field'); // custom table won't be queried | |
| ACFCDTvOneDotZeroDotAnyGetFieldInterceptBypass::disable(); | |
| the_field('some_third_field'); // custom table will be queried | |