Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have

<?php 
$folioPosts = get_posts(array('category_name' => 'portfolio', 'numberposts' => 3));
if ($folioPosts->have_posts()) :
foreach ($folioPosts as $folioPost) : 
setup_postdata($folioPost);
?>

<article class="col3">
  <?php if (has_post_thumbnail()) : ?>
  <p><a href="<?php the_permalink() ?>"><?php the_post_thumbnail() ?></a></p>
  <?php endif; ?>
  <p><a href="<?php the_permalink() ?>"><?php the_title() ?></a></p>
</article>

<?php
endforeach;
else :
?>
<p>No folio posts to display ...</p>
<?php endif; ?>

I am getting

Fatal error: Call to a member function have_posts() on a non-object ...

I think its because my args to get_posts are wrong. Did I do something wrong?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

get_posts() returns an array, not a WP_Query object. If you want to use have_posts() and related functions, use a "raw" WP_Query object.

share|improve this answer

If you want that to work, instead of this:

if ($folioPosts->have_posts()) :

try this:

if( !empty( $folioPosts ) ) :

Also, after the foreach line and before setup_postdata, add something like this:

global $post; $post = $folioPost;
share|improve this answer
    
Could you explain why I need $post = $folioPost and what does setup_postdata() do? I can't find this info in the docs –  Jiew Meng Feb 18 '11 at 1:08
    
setup_postdata() overwrites all the post-related globals used by the template tags except for $post. Since some template tags do make use of the global $post, that needs to be changed to reflect the current item in your custom loop. –  John P Bloch Feb 18 '11 at 11:49

Your Answer

 
discard

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.