WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

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 am working on some rather elaborate comparisons between metadata and having a bit of trouble correctly referring to results from my query. The first query to establish $team_is_home works fine but as soon as I try to capture the ID of the posts where the $team_is_homeit goes a bit haywire and I get warnings saying: "Undefined property: WP_Query::$post "

$args = array(
       'post_type' => 'match_report',
       'post_status' => 'publish',
       'meta_query' => array(
                 'key' => 'report_home-select',
                 'value' => $team_id,
                 'compare' => '=',

    $hometeams = new WP_Query($args);       
    $team_is_home =  $hometeams->found_posts;
    $scorehome = get_post_meta($hometeams->post->ID, 'report_homescore'); 

How should I be reffering to $hometeams->post->ID to avoid getting this warning?

share|improve this question
up vote 4 down vote accepted

First of all post field of WP_Query is current post ID and not post object. But I don't think you should use it before calling the_post() method.

Normally you should do it in this way:

$args = ...
$hometeams = new WP_Query( $args );
$teamishome = $hometeams->have_posts();
while ( $hometeams->have_posts() ) {
  $scorehome = get_post_meta($post->ID, 'report_homescore', true); // you want only one meta, not all array I guess.
share|improve this answer
That's it. Thank you very much. I hadn't thought about it not being in its own loop (its already in another one.) Methinks this code is going to be very loopy. – mantis Jun 19 '13 at 13:45
It seems I have another problem now. Because I changed $hometeams->the_post();my first loop is no longer valid. I may have to ask another question. Arhg! – mantis Jun 19 '13 at 14:06
Add wp_reset_postdata after enwhile of your $hometeams loop. – Krzysiek Dróżdż Jun 19 '13 at 15:11
I did but it only resets that weren't affected by $hometeams. The others are reset to the ID of the page I am on. – mantis Jun 19 '13 at 15:17
I was thinking I had to reset the original query wordpress.stackexchange.com/questions/103508/cloning-a-wp-query – mantis Jun 19 '13 at 15:34

You can do it with foreach also

$args = ...
$hometeams = new WP_Query( $args );
foreach($hometeams->posts as $key => $post){
    echo get_post_meta($post->ID, 'report_homescore', true);
share|improve this answer

If you var_dump($hometeams); you will see that $hometeams->post is set to the first post in the query results even before $hometeams->the_post runs. WP_Query initializes it automatically if you have posts in the result set.

The "Undefined Property" warning occurs when your result set is empty, and thus $hometeams->post can't be set/initialized.

You need to check that you have a populated $hometeams->post before trying to use it. If you use while ( $hometeams->have_posts() ) { as suggested in one answer or foreach($hometeams->posts as $key => $post){ as in another you are looping over $hometeams->posts and so are avoiding the issue with $hometeams->post completely.

Another option would be...

if (!empty($hometeams->post)) {
  $scorehome = get_post_meta($hometeams->post->ID, 'report_homescore'); 

... however, since your query does not contain 'posts_per_page' => 1 you need to be using a loop or you will only get one of the potentially large number of posts in the result set.

Be sure to run reset_postdata after your secondary Loop to reset $post, or wp_reset_query if necessary.

share|improve this answer
I went with the loop option as it made the most sense and as I wanted to include an else statement in the event there were no posts (matches) which will be how the season will start. I'm having a hard time resetting the post query as when I use wp_reset_postdata() it resets the loop to the page I am on. I am now trying to clone the query I was thinking I had to reset the original query wordpress.stackexchange.com/questions/103508/cloning-a-wp-query – mantis Jun 19 '13 at 15:35
Yes, it probably does make the most sense to use the Loop from the accepted answer, but that answer didn't explain why exactly you were having the problem, so I did. – s_ha_dum Jun 19 '13 at 15:38
You're right, and thank you. I am having a difficult time getting my head around WP_Query. I'll certainly take any help I can get! – mantis Jun 19 '13 at 15:42

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.