From 03bd31aedd9ed8012eee7504aa5ef912623b0393 Mon Sep 17 00:00:00 2001 From: Helen Hou-Sandi Date: Fri, 20 Nov 2015 06:15:34 +0000 Subject: [PATCH] Custom fields: Allow for short-circuiting the meta key dropdown. Adds the `postmeta_form_keys` filter which allows for a potentially expensive query against postmeta to be avoided. props ericmann, tollmanz, nacin. see #33885. git-svn-id: https://develop.svn.wordpress.org/trunk@35717 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/includes/template-functions.php | 39 ++++++++++++++------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/src/wp-admin/includes/template-functions.php b/src/wp-admin/includes/template-functions.php index ecfd48901c..f85e87a8bf 100644 --- a/src/wp-admin/includes/template-functions.php +++ b/src/wp-admin/includes/template-functions.php @@ -577,21 +577,36 @@ function meta_form( $post = null ) { $post = get_post( $post ); /** - * Filter the number of custom fields to retrieve for the drop-down - * in the Custom Fields meta box. + * Filter values for the meta key dropdown in the Custom Fields meta box. * - * @since 2.1.0 + * Returning a non-null value will effectively short-circuit and avoid a + * potentially expensive query against postmeta. * - * @param int $limit Number of custom fields to retrieve. Default 30. + * @since 4.4.0 + * + * @param array|null $keys Pre-defined meta keys to be used in place of a postmeta query. Default null. */ - $limit = apply_filters( 'postmeta_form_limit', 30 ); - $sql = "SELECT DISTINCT meta_key - FROM $wpdb->postmeta - WHERE meta_key NOT BETWEEN '_' AND '_z' - HAVING meta_key NOT LIKE %s - ORDER BY meta_key - LIMIT %d"; - $keys = $wpdb->get_col( $wpdb->prepare( $sql, $wpdb->esc_like( '_' ) . '%', $limit ) ); + $keys = apply_filters( 'postmeta_form_keys', null ); + + if ( null === $keys ) { + /** + * Filter the number of custom fields to retrieve for the drop-down + * in the Custom Fields meta box. + * + * @since 2.1.0 + * + * @param int $limit Number of custom fields to retrieve. Default 30. + */ + $limit = apply_filters( 'postmeta_form_limit', 30 ); + $sql = "SELECT DISTINCT meta_key + FROM $wpdb->postmeta + WHERE meta_key NOT BETWEEN '_' AND '_z' + HAVING meta_key NOT LIKE %s + ORDER BY meta_key + LIMIT %d"; + $keys = $wpdb->get_col( $wpdb->prepare( $sql, $wpdb->esc_like( '_' ) . '%', $limit ) ); + } + if ( $keys ) { natcasesort( $keys ); $meta_key_input_id = 'metakeyselect';