diff --git a/src/wp-includes/general-template.php b/src/wp-includes/general-template.php index 640bc54c8e754..68baee9d31373 100644 --- a/src/wp-includes/general-template.php +++ b/src/wp-includes/general-template.php @@ -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 $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'] ); diff --git a/tests/phpunit/tests/functions/wpGetArchives.php b/tests/phpunit/tests/functions/wpGetArchives.php index b12fe262e83a1..615beb49fdba1 100644 --- a/tests/phpunit/tests/functions/wpGetArchives.php +++ b/tests/phpunit/tests/functions/wpGetArchives.php @@ -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( '
  • %s
  • ', 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 ); + } }