Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

When you use a standard custom field in WP you have to write the name of the cusotm fields the first time.

The second time you use a custom field it sticks to the custom fields dropdown list. But, sometimes when you install plugins they ise ALOT of custom fields.

This list becomes a bit long an it may "push" som of my custom fields out of tghe list. So, does anyone know how I can remove custom fields from my dropdown list in any easy way?

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

You can filter the query for these keys on'query'. I would start that filter as late as possible to avoid side effect.

Sample code, tested:

add_filter( 'postmeta_form_limit', 'wpse_73543_hide_meta_start' );

function wpse_73543_hide_meta_start( $num )
{
    add_filter( 'query', 'wpse_73543_hide_meta_filter' );

    return $num;
}

function wpse_73543_hide_meta_filter( $query )
{
    // Protect further queries.
    remove_filter( current_filter(), __FUNCTION__ );

    $forbidden = array ( 'the', 'keys', 'you', 'want', 'to', 'hide' );
    $where     = "WHERE meta_key NOT IN('" . join( "', '", $forbidden ) . "') ";
    $find      = "GROUP BY";
    $query     = str_replace( $find, "$where\n$find", $query );

    return $query;
}
share|improve this answer
    
then what is the purpose of the meta_form function found in wp-admin/includes/template.php? it doesn't appear to use the is_protected_meta filter. –  Milo Nov 21 '12 at 23:59
    
@Milo No, that happens before meta_form(); is reached. I have added that function to my answer. –  toscho Nov 22 '12 at 0:06
    
I think that's just to disallow editing of meta data that a user doesn't have permission to modify, it appears meta_form is where the dropdown is created via a direct sql query of existing keys. –  Milo Nov 22 '12 at 0:13
    
@Milo You were right, I have rewritten my answer completely. Thanks for the hints. :) –  toscho Nov 22 '12 at 0:34
add comment

There isn't a way to filter what appears there, you can see where it queries for keys and renders that field in the function meta_form() in wp-admin/includes/template.php, however you can see that it ignores any key prefixed with an underscore:

HAVING meta_key NOT LIKE '\_%'

this is what plugins should be doing with any meta keys they create to hide those keys from the menu.

You'll also see a filter in there, postmeta_form_limit, which is set to 30 by default. You could increase this number so your keys at least get included in the list and don't fall off the end.

Another option is to create your own meta box to manage specific meta data, rather than using the Custom Fields meta box.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.