cloning a WP_Query

时间:2013-06-19 作者:mantis

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

$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 } ?>

1 个回复
SO网友:s_ha_dum

你不应该“收回你的查询”。您的查询都存储在不同的对象中--$team, $reports_played, 原件$wp_query, 等等。只需访问您需要使用的特定对象。

这是未经测试的,但要获得如下函数the_title 工作,运行$query_object_name->the_post() 在每个循环迭代的顶部$post = $parent_object_name->post 在每个子循环之后和返回父循环之前global 将对象发布回父对象的$post. 我想那会有用的。wp_reset_postdata 将重置为主查询,因此我怀疑它是否适用于深度嵌套的查询。

结束

相关推荐

Child Pages Loop

如果有人能帮忙那就太好了。我找到了一段我使用过的代码,它基本上完成了我希望它完成的任务,列出了父级的子页面,如果有缩略图,则添加缩略图,并添加了自定义摘录。然而,问题是我不能添加个人<?php post_class(); ?> 对于循环中的div,它使用来自父级的类并对子页重复。<?php $child_pages = $wpdb->get_results(\"SELECT * FROM $wpdb->posts WHERE post_parent = \".$pos