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'm working with several nested loops in order to create a statistics table for teams in a league and I'm having trouble getting my original query back. I've read that I should clone the original query but can't find an explanation of how to do it or where to put it. Here's what I've come up with (definitely not working):

$team = new WP_Query(array('post_type' => 'team_page') ); 
$i=0;

if ( $team->have_posts() ) {
while ( $team->have_posts() ) { 
    $team->the_post();

$team_id = $team->posts[$i]->ID;        
$args = array(
       'post_type' => 'match_report',
       'post_status' => 'publish',
       'meta_query' => array(
         'relation' => 'OR',
              array(
                 'key' => 'report_home-select',
                 'value' => $team_id,
                 'compare' => '=',
              ),
              array(
                 'key' => 'report_away-select',
                 'value' => $team_id,
                 'compare' => '='
              )
       ),
        array(
            'key' => 'report_type',
            'value' => 'cup',
            'compare' => '='                  ),                                  
     );

    $reports_played = new WP_Query($args); 
    $played = $reports_played->found_posts;
    $i++;   


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

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

    $awayteams = new WP_Query($args);       
    $team_is_away =  $awayteams->found_posts;

$temp_query = clone $wp_query;

if ( $hometeams->have_posts() ) {
while ( $hometeams->have_posts() ) {
$hometeams->the_post();
$scorehome = get_post_meta($post->ID, 'report_homescore', true);
}
} else {
$scorehome = 0;
}
wp_reset_postdata();
$wp_query = clone $temp_query; ?>           

                    <tr>

                        <td class="teamname"><?php the_title(); ?></td>

                        <td><?php echo $played;?></td>                  

                        <td>9</td><!-- won -->

                        <td>0</td><!-- draw -->

                        <td>2</td><!-- lost -->

                        <td class="goalinfo"></td><!-- goals for -->

                        <td class="goalinfo">10</td><!-- goals against -->

                        <td class="goalinfo">33</td><!-- goal difference -->

                        <td class="total">27</td><!-- goal total -->
                    </tr>

 <?php } ?>
share|improve this question
add comment

1 Answer

You shouldn't have to "get your query back". Your queries are all stored in different objects-- $team, $reports_played, the original $wp_query, etc. Just access the particular object that you need to use.

This is untested but to get functions like the_title working, run $query_object_name->the_post() at the top of each Loop iteration and $post = $parent_object_name->post after each sub-Loop and before returning to the parent Loop to put the global post object back to the parent object's $post. I think that will work. wp_reset_postdata will reset to the main query so I doubt it will work for deeply nested queries.

share|improve this answer
    
I see. I realise now that I was using the_title(); where I should have been using $team->posts[$i]->post_title; Thanks once again for your help. So just out of curiosity when would I need to clone a query? I read about it here. –  mantis Jun 19 '13 at 16:24
    
You only (well, mostly only) need the clone the query when you need to overwrite $wp_query, which should be very rarely-- most likely to get pagination working with a secondary query but even in that circumstance there is probably another way to proceed. –  s_ha_dum Jun 19 '13 at 16:27
    
So if I really wanted to use the_title(); that would be the way to do it? –  mantis Jun 19 '13 at 16:32
    
I tried testing it (just for fun) and got a fatal error. Actually, I was just curious and thought it might help with testing. Thank you for your suggestions. I'm sure I will be back for more in the course of all this. –  mantis Jun 19 '13 at 20:02
    
@mantis If you got a fatal error, why is it marked as solution then? –  kaiser Jun 19 '13 at 21:13
show 2 more comments

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.