Using WordPress ‘get_media_item_args’ PHP filter

The get_media_item_args WordPress PHP filter allows you to modify the arguments used to retrieve an image for the edit image form.

Usage

add_filter('get_media_item_args', 'your_custom_function');
function your_custom_function($parsed_args) {
    // your custom code here
    return $parsed_args;
}

Parameters

  • $parsed_args (array): An array of arguments for retrieving an image in the edit image form.

More information

See WordPress Developer Resources: get_media_item_args

Examples

Change the orderby argument

Modify the arguments to change the orderby parameter to ‘title’:

add_filter('get_media_item_args', 'change_orderby_to_title');
function change_orderby_to_title($parsed_args) {
    $parsed_args['orderby'] = 'title';
    return $parsed_args;
}

Limit the number of images

Limit the number of images retrieved to 5:

add_filter('get_media_item_args', 'limit_images_to_five');
function limit_images_to_five($parsed_args) {
    $parsed_args['posts_per_page'] = 5;
    return $parsed_args;
}

Show only images with a specific tag

Display only images that have the tag ‘featured’:

add_filter('get_media_item_args', 'show_only_featured_images');
function show_only_featured_images($parsed_args) {
    $parsed_args['tag'] = 'featured';
    return $parsed_args;
}

Change the order of the images

Change the order of the images to ascending:

add_filter('get_media_item_args', 'change_image_order_to_ascending');
function change_image_order_to_ascending($parsed_args) {
    $parsed_args['order'] = 'ASC';
    return $parsed_args;
}

Show only images from a specific category

Display images only from the ‘landscapes’ category:

add_filter('get_media_item_args', 'show_only_landscapes_category');
function show_only_landscapes_category($parsed_args) {
    $parsed_args['category_name'] = 'landscapes';
    return $parsed_args;
}

“END”