2

I have more than 500 posts and I want to limit only 100 posts with pagination. So, 10 pages with 10 posts in each. Presenting you the code:

$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$posts = get_posts( 
    array(
        'numberposts' => 10,
        'paged'       => $paged,
        'meta_key'    => 'property',
        'orderby'     => 'meta_value_num', //or 'meta_value_num'
        'order'       => 'DESC',
    )
);
if( $posts ) {
    $count = 0;
    foreach( $posts as $post ) {
        setup_postdata( $post );
        // post stuff
        $count++; 
    }
    wp_reset_postdata();
}

3 Answers 3

3

Before I start, do not use get_posts for paginated queries. get_posts legally breaks pagination, and does not return the query object. If you need to paginate queries, use WP_Query

As for your issue, I really don't think limiting the total overall amount of posts to only 100 is possible when you involve pagination. The only scenarios I can think of is:

  • Remove the page links on page 10, but again you can manually enter page 11 and see page 11 posts

  • Run a custom function when a new post is published to change the post status of 101st post and sequencial posts to something else, like draft. By default, WP_Query only queries published posts for all users and extra private posts for logged in users. So if you change post status, these posts will be excluded

  • Another, less intrusive method will be to run two queries. The first one, which will be very basic and lean, will return the post ID's only of the first 100 posts. This ID's will be passed to the second query which will get 10 posts per page and have only 10 pages because we just pass 100 ID's to it. The advantage here is, if you manually enter page 11, you'll get a 404 page.

EXAMPLE OF POINT 3

As I have said, point 3 is the best option here. We will use get_posts for the first query as it cuts out pagination, which speeds up your query, and we will only get the post ID's which makes the complete query 99.9% faster than normal. Overall, the impact on the page is nearly zero. For our second query we will use WP_Query and the post__in parameter to get our paged list of posts

(This code is untested and requires PHP 5.4+)

$args = [
    'posts_per_page' => 100,
    'fields' => 'ids',
    // Add additional args here
];
$post_ids = get_posts( $args );

if ( $post_ids ) {
    $args_2 = [
        'paged' => $paged,
        'post__in' => $post_ids,
        'posts_per_page' => 10,
    ];
    $q = new WP_Query( $args_2 );
    while ( $q->have_posts() ) {
    $q->the_post();
        // YOUR LOOP
    }
    next_posts_link( 'Next Posts', $q->max_num_pages );
    previous_posts_link( 'Previous Posts' ); 
    wp_reset_postdata();
}
1
  • how could I "Remove the page links on page 10" ? you can manually enter page 11 and see page 11 posts: not a problem for me as long as google is not directed to page 11 Commented May 24, 2016 at 17:09
5

Maybe it helps

function wpcodex_filter_main_search_post_limits( $limit, $query ) {     
  return 'LIMIT 0, 100'; 
} 
add_filter( 'post_limits', 'wpcodex_filter_main_search_post_limits', 10, 2 );

More: post_limits filter

2
  • Thanks Pieter. This is what i was looking for and i am sorry, i have less reputation to vote this answer. Commented Jul 15, 2015 at 19:14
  • Please mark it as acceptable by the author. I remember you can do it. Thanks.
    – Trang
    Commented Jul 16, 2015 at 10:30
0

Try this code that will helps you display only 10 pages.

This is code where you want to display the post:

<?php 
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'post_type' => 'property', 'posts_per_page' => 10,'paged'=> $paged,'orderby'=> 'meta_value_num','order'=> 'DESC');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
print_r($post); ?>
<h1> <?php the_post_thumbnail(); ?></h1>
<h2><?php the_title(); ?></h2>
<h3><?php the_content(); ?></h3>
<?php endwhile; ?>

This is footer where you need to display pagination.

<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
<?php if($loop->max_num_pages > 1 && $loop->max_num_pages < 11 ) { ?>
<ul class="pager">
<?php for($i=1;$i<=$loop->max_num_pages; $i++){ ?>
<li><a href="<?php  get_permalink();?>page/<?php echo $i; ?>"> <?php echo $i; ?></a></li>
<?php } ?>
</ul>
<?php } else if($loop->max_num_pages > 1 && $loop->max_num_pages > 10) {  ?>
<ul class="pager">
<?php for($i=1;$i<=10; $i++){ ?>
<li><a href="<?php  get_permalink();?>page/<?php echo $i; ?>"> <?php echo $i; ?></a></li>
<?php } ?>
</ul>
<?php } else{ ?>

<?php } ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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