I have

$folioPosts = get_posts(array('category_name' => 'portfolio', 'numberposts' => 3));
if ($folioPosts->have_posts()) :
foreach ($folioPosts as $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>

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?


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.


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;
  • 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

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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