Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 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
18 changes: 13 additions & 5 deletions src/wp-includes/general-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -2031,9 +2031,19 @@ function wp_get_archives( $args = '' ) {
$parsed_args['type'] = 'monthly';
}

if ( ! empty( $parsed_args['limit'] ) ) {
$parsed_args['limit'] = absint( $parsed_args['limit'] );
$parsed_args['limit'] = ' LIMIT ' . $parsed_args['limit'];
/**
* Filters the limit for the number of posts to include in the archive.
*
* @since 7.0.0
*
* @param int $limit The limit for the number of posts to include in the archive. Default 0.
* @param array $parsed_args An array of default arguments.
*/
$limit_number = (int) apply_filters( 'getarchives_limit', absint( $parsed_args['limit'] ), $parsed_args );
if ( $limit_number > 0 ) {
$limit = " LIMIT $limit_number";
} else {
$limit = '';
}

$order = strtoupper( $parsed_args['order'] );
Expand Down Expand Up @@ -2070,8 +2080,6 @@ function wp_get_archives( $args = '' ) {

$last_changed = wp_cache_get_last_changed( 'posts' );

$limit = $parsed_args['limit'];

if ( 'monthly' === $parsed_args['type'] ) {
$query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date $order $limit";
$key = md5( $query );
Expand Down
49 changes: 49 additions & 0 deletions tests/phpunit/tests/functions/wpGetArchives.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,53 @@ public function test_wp_get_archives_post_type() {
);
$this->assertSame( $expected, trim( $archives ) );
}

/**
* @ticket 64304
*/
public function test_wp_get_archives_limit_filter() {
$ids = array_slice( array_reverse( self::$post_ids ), 0, 3 );

$title1 = get_post( $ids[0] )->post_title;
$title2 = get_post( $ids[1] )->post_title;
$title3 = get_post( $ids[2] )->post_title;

// Test without filter - should return all 3 posts when limit is 3.
$archives_without_filter = wp_get_archives(
array(
'echo' => false,
'type' => 'postbypost',
'limit' => 3,
)
);
$this->assertStringContainsString( $title1, $archives_without_filter );
$this->assertStringContainsString( $title2, $archives_without_filter );
$this->assertStringContainsString( $title3, $archives_without_filter );

// Add filter to modify limit to 2.
add_filter(
'getarchives_limit',
function ( $limit, $parsed_args ) {
// Modify limit from 3 to 2.
return ' LIMIT 2';
Comment thread
jeherve marked this conversation as resolved.
Outdated
},
10,
2
);

// Test with filter - should return only 2 posts.
$archives_with_filter = wp_get_archives(
array(
'echo' => false,
'type' => 'postbypost',
'limit' => 3,
)
);
$this->assertStringContainsString( $title1, $archives_with_filter );
$this->assertStringContainsString( $title2, $archives_with_filter );
$this->assertStringNotContainsString( $title3, $archives_with_filter );

// Remove filter.
remove_all_filters( 'getarchives_limit' );
Comment thread
jeherve marked this conversation as resolved.
Outdated
}
}
Loading