我正在处理几个嵌套循环,以便为联盟中的球队创建一个统计表,但我很难恢复原始查询。我读到应该克隆原始查询,但找不到关于如何执行或将其放置在何处的解释。以下是我的想法(肯定不起作用):
$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 } ?>