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

Filters the LIMIT clause of the query.


Parameters Parameters

$limits

(string) The LIMIT clause of the query.

$query

(WP_Query) The WP_Query instance (passed by reference).


Top ↑

More Information More Information

  • 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.

Top ↑

Source Source

File: wp-includes/class-wp-query.php

View on Trac



Top ↑

Changelog Changelog

Changelog
Version Description
2.1.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by stevenlinx

    Example migrated from Codex:

    The example below allows your query to return 25 results only for the search page. All other queries will continue to return the default value.

    /**
     * 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
     * this query is a search with $this->is_search().
     *
     * @see https://developer.wordpress.org/reference/hooks/post_limits/
     * 
     * @param string $limit The 'LIMIT' clause for the query.
     * @param object $this The current query object.
     *
     * @return string The filtered LIMIT.
     */
    function wpcodex_filter_main_search_post_limits( $limit, $this ) {
    
    	if ( ! is_admin() && $this->is_main_query() && $this->is_search() ) {
    		return 'LIMIT 0, 25';
    	}
    
    	return $limit;
    }
    add_filter( 'post_limits', 'wpcodex_filter_main_search_post_limits', 10, 2 );
    

Top ↑

You must log in before being able to contribute a note or feedback.