Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/wp-admin/includes/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -2869,12 +2869,11 @@ function media_upload_library_form( $errors ) {

<div class="alignleft actions">
<?php
$months = $wpdb->get_results(
"SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month
FROM $wpdb->posts
WHERE post_type = 'attachment'
ORDER BY post_date DESC"
);
/** This filter is documented in wp-includes/media.php */
$months = apply_filters( 'media_library_months_with_files', null );
if ( ! is_array( $months ) ) {
$months = wp_get_media_library_attachment_months();
}

$month_count = count( $months );
$selected_month = isset( $_GET['m'] ) ? (int) $_GET['m'] : 0;
Expand Down
42 changes: 33 additions & 9 deletions src/wp-includes/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -4883,16 +4883,9 @@ function wp_enqueue_media( $args = array() ) {
*/
$months = apply_filters( 'media_library_months_with_files', null );
if ( ! is_array( $months ) ) {
$months = $wpdb->get_results(
$wpdb->prepare(
"SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month
FROM $wpdb->posts
WHERE post_type = %s
ORDER BY post_date DESC",
'attachment'
)
);
$months = wp_get_media_library_attachment_months();
}

foreach ( $months as $month_year ) {
$month_year->text = sprintf(
/* translators: 1: Month, 2: Year. */
Expand Down Expand Up @@ -5152,6 +5145,37 @@ function wp_enqueue_media( $args = array() ) {
do_action( 'wp_enqueue_media' );
}

/**
* Retrieves the months that have media library attachments.
*
* Example:
*
* $months = wp_get_media_library_attachment_months();
* $months === array(
* (object) array( 'year' => '2025', 'month' => '2' ),
* (object) array( 'year' => '2024', 'month' => '11' ),
* );
*
* @since tbd
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @return array<int, object{year: string, month: string}> Months with associated attachment post dates.
*/
function wp_get_media_library_attachment_months(): array {
global $wpdb;

return $wpdb->get_results(
$wpdb->prepare(
"SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month
FROM $wpdb->posts
WHERE post_type = %s
ORDER BY post_date DESC",
'attachment'
)
);
}

/**
* Retrieves media attached to the passed post.
*
Expand Down
38 changes: 38 additions & 0 deletions tests/phpunit/tests/media/wpGetMediaLibraryAttachmentMonths.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/**
* Tests for the `wp_get_media_library_attachment_months()` function.
*
* @group media
* @covers ::wp_get_media_library_attachment_months
*/
class Tests_Media_wpGetMediaLibraryAttachmentMonths extends WP_UnitTestCase {

/**
* Tests that the function returns the correct months.
*
* @ticket 63279
*/
public function test_returns_months() {
self::factory()->post->create(
array(
'post_type' => 'attachment',
'post_date' => '2025-03-15 00:00:00',
)
);
self::factory()->post->create(
array(
'post_type' => 'attachment',
'post_date' => '2024-11-01 00:00:00',
)
);

$months = wp_get_media_library_attachment_months();

$this->assertCount( 2, $months );
$this->assertSame( '2025', $months[0]->year );
$this->assertSame( '3', $months[0]->month );
$this->assertSame( '2024', $months[1]->year );
$this->assertSame( '11', $months[1]->month );
}
}
Loading