Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Custom Fields Meta Box: Define Meta Keys for Performance
<?php
/**
* Plugin Name: Custom Fields Meta Box: Define Meta Keys for Performance
* Plugin URI: http://www.wpzinc.com/
* Version: 0.0.1
* Author: WP Zinc
* Author URI: http://www.wpzinc.com
* Description: Explicitly define the Meta Keys to display in the Custom Fields > Add New Custom Field > Name <select> dropdown, to save WordPress performing a potentially expensive and slow query.
*/
/**
* Defines the Meta Keys to display in the <select> dropdown.
*
* If null is returned, WordPress will perform a DB query to fetch all unique
* meta keys from the Post Meta table, which can be a slow and expensive
* query if the WordPress installations contains a lot of post meta data.
*
* @since 0.0.1
*
* @param array $meta_keys Meta Keys
* @param WP_Post $post WordPress Post
* @return array Meta Keys
*/
function custom_fields_meta_box_meta_keys( $meta_keys, $post ) {
// Define the meta keys that you want to return
// You must specify at least one
$keys = array(
'key1',
'key2',
);
// Stop editing
return $keys;
}
add_filter( 'postmeta_form_keys', 'custom_fields_meta_box_meta_keys', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.