The post_limits_request WordPress PHP filter allows you to modify the LIMIT clause of a query, typically used by caching plugins.
Usage
add_filter('post_limits_request', 'my_custom_post_limits_request', 10, 2); function my_custom_post_limits_request($limits, $query) { // your custom code here return $limits; }
Parameters
- $limits (string): The LIMIT clause of the query.
- $query (WP_Query): The WP_Query instance (passed by reference).
More information
See WordPress Developer Resources: post_limits_request
Examples
Limit posts to the first 5
In this example, we limit the query to return only the first 5 posts.
function limit_posts_to_five($limits, $query) { $limits = 'LIMIT 5'; return $limits; } add_filter('post_limits_request', 'limit_posts_to_five', 10, 2);
No limit on archive pages
In this example, we remove the post limit on archive pages.
function no_limit_on_archive($limits, $query) { if ($query->is_archive()) { $limits = ''; } return $limits; } add_filter('post_limits_request', 'no_limit_on_archive', 10, 2);
Limit posts to 10 on search results
In this example, we limit the search results to display only 10 posts.
function limit_search_results($limits, $query) { if ($query->is_search()) { $limits = 'LIMIT 10'; } return $limits; } add_filter('post_limits_request', 'limit_search_results', 10, 2);
Custom limit based on post type
In this example, we limit posts based on their post type.
function custom_limit_by_post_type($limits, $query) { if ($query->is_post_type_archive('portfolio')) { $limits = 'LIMIT 6'; } elseif ($query->is_post_type_archive('news')) { $limits = 'LIMIT 8'; } return $limits; } add_filter('post_limits_request', 'custom_limit_by_post_type', 10, 2);
Increase limit for logged-in users
In this example, we increase the post limit for logged-in users.
function increase_limit_for_logged_users($limits, $query) { if (is_user_logged_in()) { $limits = 'LIMIT 15'; } return $limits; } add_filter('post_limits_request', 'increase_limit_for_logged_users', 10, 2);