post_limitsfilter-hookWP 2.1.0

Changes the LIMIT clause of the SQL query before posts are retrieved from the database by WP_Query.

The hook lets you define the required LIMIT restriction in a post query.

When changing post_limits, remember to account for pagination.

To remove the query's LIMIT clause, return NULL from the filter. The query then returns all matching results without a limit. However, $wp_query->found_posts will be 0 in this case.

Usage

add_filter( 'post_limits', 'filter_function_name_11' );
function filter_function_name_11( $limit ) {
	// Process the supplied data...

	return $limit;
}

Parameters

$limit(string)
A string containing part of the SQL query. For example: LIMIT 0, 25.

Examples

#1 Number of posts displayed in search results

A demonstration of how to change the number of posts displayed on a site search page. This example sets the result count to 25:

add_filter( 'post_limits', 'my_post_limits' );
function my_post_limits( $limit ) {
	if ( is_search() ) {
		return 'LIMIT 0, 25';
	}
	return $limit;
}

This is only a demonstration and should not be used as-is. The code disables pagination because search pages always receive LIMIT 0, 25. To change the post count without breaking pagination, it is better to set the posts_per_page parameter through the pre_get_posts action hook.

#2 Change the number of posts displayed in an RSS feed

The number of posts displayed in a feed can usually be changed in the settings. When that option is unsuitable, use code like this:

add_filter( 'post_limits', 'how_many_posts_display_in_feed' );
function how_many_posts_display_in_feed($query) {
	if( ! is_feed() )
		return;

	return 'LIMIT 25';
}

Changelog

Since 2.1.0 Introduced.

Where the hook is called

WP_Query::get_posts()
post_limits
wp-includes/class-wp-query.php 2999
$limits = apply_filters_ref_array( 'post_limits', array( $limits, &$this ) );

Where the hook is used in WordPress

Usage not found.