Skip to content

Conversation

@jeherve
Copy link

@jeherve jeherve commented Nov 25, 2025

wp_get_archives already comes with 2 filters allowing one to customize some of the aspects of the queries to fetch archives on a site:

  • getarchives_where allows you to customize the WHERE clause
  • getarchives_join allows you to customize the JOIN clause

I think it would be useful to add a new filter, to allow customizing the LIMIT clause as well. In some environments, with sites with lots of posts with longform content, some of those queries can be really resource-intensive.

Note

I have 2 notes about my PR:

  1. I used 7.0.0 as the since value. I'm not sure if I'm supposed to use a placeholder until this is ready for commit.
  2. I used the same filter prefix as the other filters in the function, even though they do not follow the typical wp_ prefix of other, newer filters. Let me know if I should change that.

Trac ticket: https://core.trac.wordpress.org/ticket/64304

This new filter will allow site admins to customize the LIMIT clause for all wp_get_archives requests on their site.

Trac ticket: https://core.trac.wordpress.org/ticket/64304
@github-actions
Copy link

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props jeherve.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions
Copy link

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Comment on lines +2078 to +2079
* @param string $limit The limit of the query for the `wp_get_archives` function.
* @param array $parsed_args An array of default arguments.
Copy link
Member

Choose a reason for hiding this comment

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

Nit: The descriptions should be aligned.

Suggested change
* @param string $limit The limit of the query for the `wp_get_archives` function.
* @param array $parsed_args An array of default arguments.
* @param string $limit The limit of the query for the `wp_get_archives` function.
* @param array $parsed_args An array of default arguments.

* @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.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants