Prevent the postmeta_form_keys query from running on Product or Order admin pages within WordPress.
When creating or editing a post type in WordPress, a query against the post meta table is ran that will populate the Custom Fields meta boxes. These can be toggled on and off using the Screen Options. However, the query is still ran when the Custom Fields are toggle off. The query becomes somewhat inefficient when the postmeta table gets "large" (4 million+ rows in our case), the query can take 10 to 20 seconds.
This is what the query looks like by default.
SELECT DISTINCT meta_key
FROM wp_postmeta
WHERE meta_key NOT BETWEEN '_' AND '_z'
HAVING meta_key NOT LIKE '_%'
ORDER BY meta_key
LIMIT 30WordPress provides the postmeta_form_keys filter allowing you to "pre-fetch" your own meta keys. By hooking into this filter and returning an empty array, WordPress will skip the costly query. In our case, we weren't using the custom post meta fields, so there was no harm done.
- Download the latest release
- Follow the instructions provided by the WordPress documentation.
I usually develop on Mac using terminal. This is probably different on Windows.
- Clone the repository into the
pluginsdirectory of your WordPress develop site. - Create a copy of the
.makenvfile.cp .makenv.sample .makenv. - Update
.makenvwith your test database information. - Run
make setup-tests - Run
composer testto run integration tests. - Run
composer allto run php linting, WordPress code standards, and phpunit all in one.