Because of the lack of documentation about post_limits, its hard to learn more about this function and when to use it. Never used it before or seen it used so maybe its not to be used?

function wpcodex_filter_home_post_limits( $limit, $query ) {

    if ( ! is_admin() && $query->is_main_query() && $query->is_home() ) {
        return 'LIMIT 0, 25';
    }

    return $limit;
}
add_filter( 'post_limits', 'wpcodex_filter_home_post_limits', 10, 2 );

Based on my testing, post_limits does the same thing as using pre_get_posts

If i wanted to return 5 posts conditionally without paginating the remainder, would i use post_limits or pre_get_posts?

2 s
2

Note that here you’re overriding the paging of the main query, with the posts_limits filter, by using hardcoded values:

'LIMIT 0, 25'

where 0 is the offset and 25 is the number of posts to display.

So in this case I would just use pre_get_posts with

$query->set( 'posts_per_page', 25 );

and we don’t have to worry about the paging.

If i wanted to return 5 posts conditionally without paginating the
remainder, would i use post_limits or pre_get_posts?

If we later decide that pagination is necessary, then we would need to rework your posts_limits code. The pre_get_posts filter would work as is and we could therefore say that it’s at least a more “future proofed” method.

Another thing: If you were using get_posts() or WP_Query() with suppressed filters, then the posts_limits filter wouldn’t be available while the pre_get_posts hook would be accessible.

Leave a Reply

Your email address will not be published. Required fields are marked *