Limit the number of posts via wpquery

We suppose that a category has 1000 posts, we can use the following code to show 10 posts per page and the wp pagenavi would present 100 pages with links of above 1000 posts.

?php query_posts($query_string."showposts=10orderby=dateorder=ASC") ?

Is there any way that I show only 100 recent posts and hide remaining 900 posts because they may be too old? I used the numberpost but it did not work. My code is as follows:

?php query_posts($query_string."showposts=10numberposts=100orderby=dateorder=ASC") ?
?php 
$mypage = (get_query_var('paged')) ? get_query_var('paged') : 1;
$ppp = get_query_var('posts_per_page');
$counter = ($mypage * $ppp) - $ppp; 
while(have_posts()) : the_post(); 
    $counter++; // that pushes the zero based counter up one ? 
    span class="num"?php edit_post_link('♪'); ??php echo $counter;?. /span
?php echo get_the_title(); ?
?php endwhile; ?

Thanks

Topic plugin-wp-pagenavi wp-query Wordpress

Category Web


AFAIK, there is no way limiting the amount of paged queries with what is given by default. You might be able to make use of the filters provided to alter the SQL query.

You also do not want to use query_posts at all and most probably you don't even need a custom query. If this is the main query, you would want to make use of pre_get_posts to alter the the query varaibles before the main query is executed. This way you won't have problems paginating your queries

You can, however, limit the amount of pages shown with next_posts_link. You can simply just set the amount of pages you need.

Example:

next_posts_link( 'Older Entries',10 );

Actually what you need to do is set max_num_pages to 10. So before starting your while loop, you will need to set it's value.

$wp_query->max_num_pages = 10;

Not sure if $wp_query is right here, because it's easier to set it in custom WP_Query which is recommended to use. Because query_posts usually breaks. Read more about it here.

And also, there is no such thing as showposts or numberposts now, use posts_per_page instead. So here will be your query.

<?php query_posts($query_string."&posts_per_page=10&orderby=date&order=ASC"); ?>

You can read about all accepted parameters on query_posts page in codex.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.