Filter hook 'media_library_months_with_files'

in WP Core File wp-includes/media.php at line 4874

View Source

media_library_months_with_files

Filter Hook
Description
Allows overriding the list of months displayed in the media library. By default (if this filter does not return an array), a query will be run to determine the months that have media items. This query can be expensive for large media libraries, so it may be desirable for sites to override this behavior.

Hook Information

File Location wp-includes/media.php View on GitHub
Hook Type Filter
Line Number 4874

Hook Parameters

Type Name Description
stdClass[]|null $months An array of objects with `month` and `year` properties, or `null` for default behavior.

Usage Examples

Basic Usage
<?php
// Hook into media_library_months_with_files
add_filter('media_library_months_with_files', 'my_custom_filter', 10, 1);

function my_custom_filter($months) {
    // Your custom filtering logic here
    return $months;
}

Source Code Context

wp-includes/media.php:4874 - How this hook is used in WordPress core
<?php
4869  	 * @link https://core.trac.wordpress.org/ticket/31071
4870  	 *
4871  	 * @param stdClass[]|null $months An array of objects with `month` and `year`
4872  	 *                                properties, or `null` for default behavior.
4873  	 */
4874  	$months = apply_filters( 'media_library_months_with_files', null );
4875  	if ( ! is_array( $months ) ) {
4876  		$months = $wpdb->get_results(
4877  			$wpdb->prepare(
4878  				"SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month
4879  				FROM $wpdb->posts

PHP Documentation

<?php
/**
	 * Allows overriding the list of months displayed in the media library.
	 *
	 * By default (if this filter does not return an array), a query will be
	 * run to determine the months that have media items.  This query can be
	 * expensive for large media libraries, so it may be desirable for sites to
	 * override this behavior.
	 *
	 * @since 4.7.4
	 *
	 * @link https://core.trac.wordpress.org/ticket/31071
	 *
	 * @param stdClass[]|null $months An array of objects with `month` and `year`
	 *                                properties, or `null` for default behavior.
	 */
Quick Info
  • Hook Type: Filter
  • Parameters: 1
  • File: wp-includes/media.php
Related Hooks

Related hooks will be displayed here in future updates.