post_limits

最后更新于:2021-11-27 16:44:31

apply_filters_ref_array( ‘post_limits’, string $limits, GC_Query $query )

Filters the LIMIT clause of the query.

参数

$limits

(string)
The LIMIT clause of the query.

$query

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

源文件

文件: gc-includes/class-gc-query.php

View on Trac

/**
 * 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://docs.gechiui.com/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 gccodex_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', 'gccodex_filter_main_search_post_limits', 10, 2 );