How do I change the number of results returned by a search

To change the number of results returned by a search in wordpress, insert this code on your plugin:

<?php
/*
* Alter number of search results per page to 20
*
*/
add_filter( 'pre_get_posts','wpster_search_results_per_page' );
function wpster_search_results_per_page($query) {
	if ( $query->is_search ) {
		$query->set( 'posts_per_page', '20' );
	}
	return $query;
}

/*
* Remove limit of search results returned
*
*/
add_filter( 'pre_get_posts','wpster_search_results_per_page' );
function wpster_search_results_per_page($query) {
	if ( $query->is_search ) {
		$query->set( 'posts_per_page', '-1' );
	}
	return $query;
}
?>

Submit a Comment

Your email address will not be published.