Using WordPress ‘postmeta_form_limit’ PHP filter

The postmeta_form_limit filter allows you to change the number of custom fields displayed in the drop-down menu in the Custom Fields meta box in WordPress.

Usage

add_filter('postmeta_form_limit', 'your_custom_function_name');
function your_custom_function_name($limit) {
    // your custom code here
    return $limit;
}

Parameters

  • $limit (int): Number of custom fields to display. Default is 30.

More information

See WordPress Developer Resources: postmeta_form_limit

Examples

Limit custom fields to 50

Increase the number of custom fields in the drop-down menu to 50.

add_filter('postmeta_form_limit', 'increase_postmeta_limit');
function increase_postmeta_limit($limit) {
    $limit = 50;
    return $limit;
}

Limit custom fields to 10

Decrease the number of custom fields in the drop-down menu to 10.

add_filter('postmeta_form_limit', 'decrease_postmeta_limit');
function decrease_postmeta_limit($limit) {
    $limit = 10;
    return $limit;
}

Double the custom fields limit

Double the number of custom fields displayed in the drop-down menu.

add_filter('postmeta_form_limit', 'double_postmeta_limit');
function double_postmeta_limit($limit) {
    $limit = $limit * 2;
    return $limit;
}

Set custom fields limit based on user role

Set a different limit for custom fields based on the user’s role.

add_filter('postmeta_form_limit', 'role_based_postmeta_limit');
function role_based_postmeta_limit($limit) {
    if (current_user_can('editor')) {
        $limit = 60;
    } elseif (current_user_can('author')) {
        $limit = 40;
    } else {
        $limit = 20;
    }
    return $limit;
}

Remove custom fields limit

Remove the limit on custom fields displayed in the drop-down menu.

add_filter('postmeta_form_limit', 'remove_postmeta_limit');
function remove_postmeta_limit($limit) {
    $limit = -1;
    return $limit;
}