diff --git a/docs/customization/yoast-seo/filters/custom-fields-pre-query-filter.md b/docs/customization/yoast-seo/filters/custom-fields-pre-query-filter.md new file mode 100644 index 00000000..2bc55c33 --- /dev/null +++ b/docs/customization/yoast-seo/filters/custom-fields-pre-query-filter.md @@ -0,0 +1,72 @@ +--- +id: custom-fields-pre-query-filter +title: "Yoast SEO: Short-circuit the custom-fields lookup" +sidebar_label: Short-circuit custom-fields lookup +--- +Yoast SEO populates the custom-field autocomplete in the settings meta box by running a `SELECT DISTINCT meta_key` query against the `wp_postmeta` table. On sites with very large postmeta tables this query can become expensive. + +The `wpseo_custom_fields_pre_query` filter lets you short-circuit that query. When the filter callback returns a non-`null` value, the database query is skipped entirely and the returned array is used instead. + +## Parameters + +- `string[]|null $custom_fields` — Pre-computed list of meta key names, or `null` to run the default query. +- `int $limit` — The configured result limit (default 30, from the `postmeta_form_limit` filter). Honor this value if running a custom query. + +## Example: supply a hardcoded list + +The simplest use case is returning a fixed list of known custom fields to avoid the database query entirely: + +```php +