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.

When I do

$gallerypage = new WP_Query(array(
    'post_type' => 'post',
    'pagename' => 'gallery'
));

$gallerypage->the_post();

the last line $gallerypage->the_post(); is giving me errors. Why is that? It seems correct?

Notice: Undefined offset: 0 in /site/url/wp-includes/query.php on line 2719
Notice: Trying to get property of non-object in /site/url/wp-includes/query.php on line 3491
Notice: Trying to get property of non-object in /site/url/wp-includes/query.php on line 3493
Notice: Trying to get property of non-object in /site/url/wp-includes/query.php on line 3495
Notice: Trying to get property of non-object in /site/url/wp-includes/query.php on line 3496
Notice: Trying to get property of non-object in /site/url/wp-includes/query.php on line 3503
Notice: Trying to get property of non-object in /site/url/wp-includes/query.php on line 3514
Notice: Trying to get property of non-object in /site/url/wp-includes/post-template.php on line 30
Notice: Trying to get property of non-object in /site/url/wp-includes/post-template.php on line 30

wp-includes/query.php line 2717 onwards

$this->current_post++;

$this->post = $this->posts[$this->current_post]; // error here?
share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

Hi @JM at Work:

Try wrapping $gallerypage->the_post(); with if ($gallerypage->have_posts()): ... endif;

It should look like this:

if ($gallerypage->have_posts()):
  $gallerypage->the_post();
endif; 

See:

share|improve this answer
    
Thanks, I was expecting the page to exists so I skipped that for simplicity, but backfired ... I should be searching for a "page" instead of a "post" –  JM at Work Apr 5 '11 at 5:28
    
@JM at Work - So my reply answered your question, or not? –  MikeSchinkel Apr 5 '11 at 5:36
    
Yes, I forgot to mark it as answer –  JM at Work Apr 5 '11 at 5:37
    
@JM at Work - Thanks! Glad I could help. –  MikeSchinkel Apr 5 '11 at 6:46
add comment

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.