Skip to content

Repository files navigation

Exclude Postmeta Form Keys for WooCommerce

Prevent the postmeta_form_keys query from running on Product or Order admin pages within WordPress.

PHP Tests PHP Code Standards PHP Lint

The Problem

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 30

The Solution

WordPress 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.

Installation

  1. Download the latest release
  2. Follow the instructions provided by the WordPress documentation.

Development

I usually develop on Mac using terminal. This is probably different on Windows.

  1. Clone the repository into the plugins directory of your WordPress develop site.
  2. Create a copy of the .makenv file. cp .makenv.sample .makenv.
  3. Update .makenv with your test database information.
  4. Run make setup-tests
  5. Run composer test to run integration tests.
  6. Run composer all to run php linting, WordPress code standards, and phpunit all in one.

About

Prevent a slow query during product or order creation and editing within wp-admin

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages