The third parameter for add_filter
is the priority in which the filter is executed, the forth is the number of parameters the function in the second parameter uses.
The wpcodex_filter_main_search_post_limits
function has an if that checks uses a couple of wordpress function to establish where in your page you are executing the code, in this case it’s checking if it’s in the frontend, if it’s the main query and if it’s in the search page.
The parameter $limit
and $query
are passed by the CMS, and those are default values, so if the conditions in the if aren’t met, the function return the default $limit
edit:
function posts_limit_fn($limit, $query){
return 'LIMIT 0, 3';
}
add_filter('posts_limit', 'posts_limit_fn',10, 2);
edit 2:
if you aren’t going to use the parameters you can use the function like this:
global $custom_limit;
$custom_limit="0, 2";
function posts_limit_fn(){
global $custom_limit
return 'LIMIT ' . $custom_limit;
}
add_filter('posts_limit', 'posts_limit_fn',10, 0);
That’s one way to do it. Try to prefix all global vars, always, and if you can avoid them, better yet.
edit: without globals
function posts_limit_fn($limit, $query, $custom_limit=""){
if($custom_limit !== ''){
return $custom_limit;
}
return $limit;
}
add_filter('posts_limit', 'posts_limit_fn',10, 3);
$custom_limit="LIMIT 0,4";
apply_filters('posts_limit', '', '', $custom_limit)
edit for WP_Query
:
If you are using WP_Query
, for the pagination you can pass a paged
parameter with each call to the function.