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

github-actions bot commented Nov 25, 2025

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, westonruter.

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.

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

@westonruter
Copy link
Member

Something that just came to mind: instead of introducing a filer specifically for the limit arg, what if there was a getarchives_args filter instead which filters the $parsed_args? This would be similar to other such filters like:

jeherve and others added 2 commits November 26, 2025 19:53
@jeherve
Copy link
Author

jeherve commented Nov 26, 2025

instead of introducing a filer specifically for the limit arg, what if there was a getarchives_args filter instead which filters the $parsed_args?

This would work ; I suppose it would then not be consistent with the existing filters for the archives though.

@westonruter
Copy link
Member

I think that's OK. The other filters are explicitly lower-level for manipulating the SQL, whereas this limit filter is really just a subset of filtering all of the parsed args.

@jeherve
Copy link
Author

jeherve commented Nov 26, 2025

I just pushed a new commit to try that.

Copy link
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

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

Any thoughts about placement of the filter before or after the $parsed_args are collected? Core is inconsistent on this, it seems. The wp_nav_menu_args filter applies after merging the passed $args with the $defaults (and some validation), whereas the wp_terms_checklist_args filter applies before merging the args with the defaults.

return;
}

$parsed_args['post_type'] = $post_type_object->name;
Copy link
Member

Choose a reason for hiding this comment

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

This is a curious bit of code, isn't it. Wouldn't this already be the same as $parsed_args['post_type']?

Copy link
Author

Choose a reason for hiding this comment

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

I suppose that may be a way to get capitalization right? It looks like it was added in https://core.trac.wordpress.org/ticket/21596

@jeherve
Copy link
Author

jeherve commented Nov 27, 2025

Any thoughts about placement of the filter before or after the $parsed_args are collected?

I thought about it too, and figured it may be nice to have the filtering happen before, so we still come out with a consistently formatted $parsed_args.

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