WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a new question, how can I limit the amount of posts coming out of this query? I only need 7

$newsposts = new WP_Query('cat=restaurant');
if ( is_front_page()) { 
    echo '<h3 class="member-review">Latest Restaurants</h3>
    <div id="extra">';
    if ($newsposts->have_posts()) : while ($newsposts->have_posts()) : $newsposts->the_post();
        echo '<div class="reslogo"><img src="'.catch_that_image().'"/></div>';
    endwhile; endif; 
    echo '</div>';    

I tried to put: ('cat=restaurants'.'limit=7') but she no work. How did i go wrong? any help would be appreciated

share|improve this question
up vote 7 down vote accepted

It should be:

$newsposts = new WP_Query('cat=restaurant&posts_per_page=7');

Another way to write it (helps readability with larger queries) would be:

$newsposts = new WP_Query(array(
    'cat' => 'restaurant',
    'posts_per_page' => 7,

See WP_Query in Codex for description of available parameters.

PS would be good practice to add wp_reset_postdata() at the end. You are (correctly) not modifying main query, but you do change global $post variable with this loop.

share|improve this answer
Hi Rarst, thank you for your help. It makes sense - so by adding the ampersandis how I can add more conditions. I also now have a better understanding of the array key value pair. I've added your wp_reset_postdata() and will do so going forward. Awesome! – brett Apr 4 '11 at 9:32

That's what works for me (showposts):

$query = new WP_Query(array(
        'showposts' => 4, 
        'post_type' => 'xxx',
        'cache_results' => false
share|improve this answer

I think you have a slight error here, the 'cat' paramater only accepts a post ID as an Integer

Source: WP_Query Parameters

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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