Skip to content

Commit 83d27d8

Browse files
committed
Get archives: add filter to customize query LIMIT clause
This new filter will allow site admins to customize the LIMIT clause for all wp_get_archives requests on their site. Trac ticket: https://core.trac.wordpress.org/ticket/64304
1 parent 55145d5 commit 83d27d8

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

src/wp-includes/general-template.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2070,7 +2070,15 @@ function wp_get_archives( $args = '' ) {
20702070

20712071
$last_changed = wp_cache_get_last_changed( 'posts' );
20722072

2073-
$limit = $parsed_args['limit'];
2073+
/**
2074+
* Filters the SQL LIMIT clause for retrieving archives.
2075+
*
2076+
* @since 7.0.0
2077+
*
2078+
* @param string $limit The limit of the query for the `wp_get_archives` function.
2079+
* @param array $parsed_args An array of default arguments.
2080+
*/
2081+
$limit = apply_filters( 'getarchives_limit', $parsed_args['limit'], $parsed_args );
20742082

20752083
if ( 'monthly' === $parsed_args['type'] ) {
20762084
$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";

tests/phpunit/tests/functions/wpGetArchives.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,53 @@ public function test_wp_get_archives_post_type() {
204204
);
205205
$this->assertSame( $expected, trim( $archives ) );
206206
}
207+
208+
/**
209+
* @ticket 7.0.0
210+
*/
211+
public function test_wp_get_archives_limit_filter() {
212+
$ids = array_slice( array_reverse( self::$post_ids ), 0, 3 );
213+
214+
$title1 = get_post( $ids[0] )->post_title;
215+
$title2 = get_post( $ids[1] )->post_title;
216+
$title3 = get_post( $ids[2] )->post_title;
217+
218+
// Test without filter - should return all 3 posts when limit is 3.
219+
$archives_without_filter = wp_get_archives(
220+
array(
221+
'echo' => false,
222+
'type' => 'postbypost',
223+
'limit' => 3,
224+
)
225+
);
226+
$this->assertStringContainsString( $title1, $archives_without_filter );
227+
$this->assertStringContainsString( $title2, $archives_without_filter );
228+
$this->assertStringContainsString( $title3, $archives_without_filter );
229+
230+
// Add filter to modify limit to 2.
231+
add_filter(
232+
'getarchives_limit',
233+
function( $limit, $parsed_args ) {
234+
// Modify limit from 3 to 2.
235+
return ' LIMIT 2';
236+
},
237+
10,
238+
2
239+
);
240+
241+
// Test with filter - should return only 2 posts.
242+
$archives_with_filter = wp_get_archives(
243+
array(
244+
'echo' => false,
245+
'type' => 'postbypost',
246+
'limit' => 3,
247+
)
248+
);
249+
$this->assertStringContainsString( $title1, $archives_with_filter );
250+
$this->assertStringContainsString( $title2, $archives_with_filter );
251+
$this->assertStringNotContainsString( $title3, $archives_with_filter );
252+
253+
// Remove filter.
254+
remove_all_filters( 'getarchives_limit' );
255+
}
207256
}

0 commit comments

Comments
 (0)