post_limits filter

Filters the LIMIT clause of the query.

apply_filters_ref_array( 'post_limits', string $limits, WP_Query $query )

Descrption

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

/**
 * 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

To run the hook, copy the example below.

$array = apply_filters( 'post_limits', $array ); 
                         
if ( !empty( $array ) ) { 
                         
   // everything has led up to this point... 
                         
} 
  

The following example is for adding a hook callback.

// define the post_limits callback 
function filter_post_limits( $array ) { 
// make filter magic happen here... 
return $array; 
}; 

// add the filter 
add_filter( 'post_limits', 'filter_post_limits', 10, 1 ); 

To remove a hook callback, use the example below.

// remove the filter 
remove_filter( 'post_limits', 'filter_post_limits', 10, 1 ); 

 

JOIN 100,000+ SUBSCRIBER's FAMILY

 Subscribe to our weekly newsletter below and never miss the latest updates in WordPress