This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.
post_limits filter
Filters the LIMIT clause of the query.
apply_filters_ref_array( 'post_limits', string $limits, WP_Query $query )
Applied to the LIMIT clause of the query that returns the post array.
- This filter applies to the LIMIT clause of the query before the query is sent to the database, allowing you to define a new query LIMIT.
- You can return null to remove the LIMIT clause from the query, allowing you to return all results. However, this will set $wp_query->found_posts to 0.
- On some server environments, the LIMIT will be applied to all queries on the page. This results in menu items and widgets also being limited to the defined number. To only limit the number of posts on a page use the action hook pre_get_posts.
Parameters
- $limits: (string) The LIMIT clause of the query.
- $query: (WP_Query) The WP_Query instance (passed by reference).
Live Example
Example 1: Limit the main query search results to 25
We only want to filter the limit on the front end of the site, so we use is_admin() to check that we aren’t on the admin side. We also only want to filter the main query, so we check that this query is the main query with $this->is_main_query(). Finally, we only want to change the limit for searches, so we check that.
$array = apply_filters( 'weplugins_post_limits', $array ); if ( !empty( $array ) ) { // everything has led up to this point... }
Example 2: Adding a hook callback
To add a callback function for the post_limits hook, use the following example:
// define the weplugins_post_limits callback function weplugins_filter_post_limits( $array ) { // make filter magic happen here... return $array; }; // add the filter add_filter( 'weplugins_post_limits', 'weplugins_filter_post_limits', 10, 1 );
Example 3: Removing a hook callback
To remove a callback function from the post_limits hook, use the example below:
// remove the filter remove_filter( 'weplugins_post_limits', 'weplugins_filter_post_limits', 10, 1 );
Contact Us
If you need any customization or further assistance, feel free to contact us.
Explore the latest in WordPress
Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.