3

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

<?php
$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

3 Answers 3

11

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.

1
  • 1
    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, 2011 at 9:32
0

That's what works for me (showposts):

$query = new WP_Query(array(
        'showposts' => 4, 
        'post_type' => 'xxx',
        'cache_results' => false
    ));
-1

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

Source: WP_Query Parameters

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.