Skip to content
Open
11 changes: 11 additions & 0 deletions src/wp-includes/general-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -2018,6 +2018,17 @@ function wp_get_archives( $args = '' ) {
'w' => get_query_var( 'w' ),
);

/**
* Filters the arguments for displaying archive links.
*
* @since 7.0.0
*
* @see wp_get_archives()
*
* @param array<string, string|int|bool> $args Arguments.
*/
$args = apply_filters( 'wp_get_archives_args', $args );

$parsed_args = wp_parse_args( $args, $defaults );

$post_type_object = get_post_type_object( $parsed_args['post_type'] );
Expand Down
33 changes: 33 additions & 0 deletions tests/phpunit/tests/functions/wpGetArchives.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,37 @@ public function test_wp_get_archives_post_type() {
);
$this->assertSame( $expected, trim( $archives ) );
}

/**
* @ticket 64304
*/
public function test_wp_get_archives_args_filter() {
// Test that the filter can modify the limit argument.
$filter_callback = function ( $args ) {
$args['limit'] = 3;
return $args;
};
add_filter( 'wp_get_archives_args', $filter_callback );

$ids = array_slice( array_reverse( self::$post_ids ), 0, 3 );

$expected = join(
"\n",
array_map(
static function ( $id ) {
return sprintf( '<li><a href="%s">%s</a></li>', get_permalink( $id ), get_the_title( $id ) );
},
$ids
)
);
$archives = wp_get_archives(
array(
'echo' => false,
'type' => 'postbypost',
'limit' => 5, // This should be overridden by the filter to 3.
)
);

$this->assertEqualHTML( $expected, $archives );
}
}
Loading