Skip to content

Commit d427c8f

Browse files
committed
General: Add wp_get_archives_args filter to wp_get_archives().
Developed in #10552 Props jeherve, westonruter. Fixes #64304. git-svn-id: https://develop.svn.wordpress.org/trunk@61326 602fd350-edb4-49c9-b593-d223f7449a82
1 parent c710fc3 commit d427c8f

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/wp-includes/general-template.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2018,6 +2018,17 @@ function wp_get_archives( $args = '' ) {
20182018
'w' => get_query_var( 'w' ),
20192019
);
20202020

2021+
/**
2022+
* Filters the arguments for displaying archive links.
2023+
*
2024+
* @since 7.0.0
2025+
*
2026+
* @see wp_get_archives()
2027+
*
2028+
* @param array<string, string|int|bool> $args Arguments.
2029+
*/
2030+
$args = apply_filters( 'wp_get_archives_args', $args );
2031+
20212032
$parsed_args = wp_parse_args( $args, $defaults );
20222033

20232034
$post_type_object = get_post_type_object( $parsed_args['post_type'] );

tests/phpunit/tests/functions/wpGetArchives.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,39 @@ public function test_wp_get_archives_post_type() {
204204
);
205205
$this->assertSame( $expected, trim( $archives ) );
206206
}
207+
208+
/**
209+
* @ticket 64304
210+
*/
211+
public function test_wp_get_archives_args_filter() {
212+
// Test that the filter can modify the limit argument.
213+
add_filter(
214+
'wp_get_archives_args',
215+
static function ( $args ) {
216+
$args['limit'] = 3;
217+
return $args;
218+
}
219+
);
220+
221+
$ids = array_slice( array_reverse( self::$post_ids ), 0, 3 );
222+
223+
$expected = join(
224+
"\n",
225+
array_map(
226+
static function ( $id ) {
227+
return sprintf( '<li><a href="%s">%s</a></li>', get_permalink( $id ), get_the_title( $id ) );
228+
},
229+
$ids
230+
)
231+
);
232+
$archives = wp_get_archives(
233+
array(
234+
'echo' => false,
235+
'type' => 'postbypost',
236+
'limit' => 5, // This should be overridden by the filter to 3.
237+
)
238+
);
239+
240+
$this->assertEqualHTML( $expected, $archives );
241+
}
207242
}

0 commit comments

Comments
 (0)