Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
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
35 changes: 35 additions & 0 deletions tests/phpunit/tests/functions/wpGetArchives.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,39 @@ 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.
add_filter(
'wp_get_archives_args',
static function ( $args ) {
$args['limit'] = 3;
return $args;
}
);

$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