Undefined property: WP_Query::$post

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(
           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?

3 Answers
3

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() ) {
  $hometeams->the_post();
  $scorehome = get_post_meta($post->ID, 'report_homescore', true); // you want only one meta, not all array I guess.
  ...
}

Leave a Comment