2

I want to show 5 posts per page but limit total number of posts being resulted in a loop.

$args = array(
  'post_type' => 'post',
  'posts_per_page' => 5,
  'paged' => $paged
);

Suppose, I have 100 posts and the above query args will display 5 posts per page and I can see 20 pages. So, how can I limit the total number of posts being resulted so that in my condition it would only show 3 pages? I'm not needing exactly 3 pages to show but wanted to limit the total post like 25 posts, 23 posts. So, if I wanted to limit 12 posts then I can see 5 posts on first page, 5 posts on second page and remaining 2 posts on last page.

5 Answers 5

1

You can use the found_posts filter to alter the number of posts WordPress reports finding from a query.

add_filter( 'found_posts', 'wpd_found_posts', 10, 2 );
function wpd_found_posts( $found_posts, $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        return 25;
    }
}
6
  • Thanks. But it seems not working as expected. I just tried 'post_per_page'=>4 and using the filter returned 7 and it should show 3 posts on second page but showing 4 posts. Feb 3, 2017 at 6:51
  • have you verified the filter is applied? you need to change the conditions in the function above to target the query you want it applied to.
    – Milo
    Feb 3, 2017 at 7:01
  • yes. if it was not being applied the pagination wouldn't have shown just 2 pages. Feb 3, 2017 at 7:02
  • I have not encountered this issue. I'm unable to test at the moment, I will have to get back to you once I can.
    – Milo
    Feb 3, 2017 at 7:07
  • you are right, the filter doesn't seem to work as I remember it. I think you will need to use it in combination with post_limits filter to force it to truncate the query results along with the number of posts it reports finding.
    – Milo
    Feb 3, 2017 at 22:53
0

Use the post_limits hook with a priority of 2...

function my_posts_limit( $limit, $query ) {
  return 'LIMIT 0, 25';
}
add_filter( 'post_limits', 'my_posts_limit', 10, 2 );
3
  • Sorry. It's also not working... shows 25 posts on per page. Feb 3, 2017 at 8:18
  • You still need to have ` 'posts_per_page' => 5` in your query.
    – Gonzoarte
    Feb 3, 2017 at 8:49
  • I have. But doesn't work. Feb 3, 2017 at 9:07
0

post_limits

/**
 * 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 is it
 * with $query->is_main_query().
 *
 * Finally, we only want to change the limit for searches, so we check that
 * this query is a search with $query->is_search().
 *
 * @see http://codex.wordpress.org/Plugin_API/Filter_Reference/post_limits
 * 
 * @param string $limit The 'LIMIT' clause for the query.
 * @param object $query The current query object.
 *
 * @return string The filtered LIMIT.
 */
function wpcodex_filter_main_search_post_limits( $limit, $query ) {

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

    return $limit;
}
add_filter( 'post_limits', 'wpcodex_filter_main_search_post_limits', 10, 2 );
-1

You can set the post limit in two ways:

1) wp-admin > Settings > Reading

2) By passing the argument query 'numberposts' => 5
2
  • Great! But I need to do through coding as I'm building custom page. Feb 3, 2017 at 6:36
  • And I already tried with 'numberposts' and didn't work. Feb 3, 2017 at 6:43
-1

Use the 'numberposts' parameter...

$args = array(
'post_type' => 'post',
'numberposts' => 25,
'posts_per_page' => 5,
'paged' => $paged
);

Exemple at the Developer's Codex

5
  • I think it's deprecated that's why I'm not getting working... Feb 3, 2017 at 6:54
  • numberposts is the same as posts_per_page, they do the same thing.
    – Milo
    Feb 3, 2017 at 7:02
  • I see @BhojendraNepal. You are right. Then Try 'showposts' instead.
    – Gonzoarte
    Feb 3, 2017 at 7:05
  • @Gonzoarte 'showposts' also not working. Feb 3, 2017 at 7:08
  • Ok.. See my other solution using the 'post_limits' filter.
    – Gonzoarte
    Feb 3, 2017 at 7:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.