Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
10 changes: 9 additions & 1 deletion src/wp-includes/general-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -2070,7 +2070,15 @@ function wp_get_archives( $args = '' ) {

$last_changed = wp_cache_get_last_changed( 'posts' );

$limit = $parsed_args['limit'];
/**
* Filters the SQL LIMIT clause for retrieving archives.
*
* @since 7.0.0
*
* @param string $limit The limit of the query for the `wp_get_archives` function.
* @param array $parsed_args An array of default arguments.
*/
$limit = apply_filters( 'getarchives_limit', $parsed_args['limit'], $parsed_args );
Copy link
Member

@westonruter westonruter Nov 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a need to filter the actual LIMIT clause? Instead of filtering the clause, what about filtering the number for the limit? So up above instead of:

if ( ! empty( $parsed_args['limit'] ) ) {
$parsed_args['limit'] = absint( $parsed_args['limit'] );
$parsed_args['limit'] = ' LIMIT ' . $parsed_args['limit'];
}

It could be:

	/**
	 * Filters the limit for the number of posts to include in the archive.
	 *
	 * @since 7.0.0
	 *
	 * @param int   $limit       The limit for the number of posts to include in the archive. Default 0.
	 * @param array $parsed_args An array of default arguments.
	 */
	$limit_number = (int) apply_filters( 'getarchives_limit', absint( $parsed_args['limit'] ), $parsed_args );
	if ( $limit_number > 0 ) {
		$limit = " LIMIT $limit_number";
	} else {
		$limit = '';
	}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I updated the code sample to set the $limit variable.)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can certainly do that. I pushed b4c617d with your exact suggestion.

I wondered if we needed to both cast as an integer and use absint.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I personally wouldn't have used absint() to begin with.


if ( 'monthly' === $parsed_args['type'] ) {
$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";
Expand Down
49 changes: 49 additions & 0 deletions tests/phpunit/tests/functions/wpGetArchives.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,53 @@ public function test_wp_get_archives_post_type() {
);
$this->assertSame( $expected, trim( $archives ) );
}

/**
* @ticket 64304
*/
public function test_wp_get_archives_limit_filter() {
$ids = array_slice( array_reverse( self::$post_ids ), 0, 3 );

$title1 = get_post( $ids[0] )->post_title;
$title2 = get_post( $ids[1] )->post_title;
$title3 = get_post( $ids[2] )->post_title;

// Test without filter - should return all 3 posts when limit is 3.
$archives_without_filter = wp_get_archives(
array(
'echo' => false,
'type' => 'postbypost',
'limit' => 3,
)
);
$this->assertStringContainsString( $title1, $archives_without_filter );
$this->assertStringContainsString( $title2, $archives_without_filter );
$this->assertStringContainsString( $title3, $archives_without_filter );

// Add filter to modify limit to 2.
add_filter(
'getarchives_limit',
function ( $limit, $parsed_args ) {
// Modify limit from 3 to 2.
return ' LIMIT 2';
},
10,
2
);

// Test with filter - should return only 2 posts.
$archives_with_filter = wp_get_archives(
array(
'echo' => false,
'type' => 'postbypost',
'limit' => 3,
)
);
$this->assertStringContainsString( $title1, $archives_with_filter );
$this->assertStringContainsString( $title2, $archives_with_filter );
$this->assertStringNotContainsString( $title3, $archives_with_filter );

// Remove filter.
remove_all_filters( 'getarchives_limit' );
}
}
Loading