Wordpress多个wp_querys,wp_reset_postdata()无效

时间:2016-11-16 23:33:55

标签: php wordpress loops

我正在尝试在页面上运行两个循环。第一个循环运行后,它完全停止第二个循环。我查看了它,并阅读了wp_reset_postdata()和wp_reset_query(),到目前为止,这些都没有帮助过我。

我的第一个循环如下:

<?php

$args = array(
    'post_type' => 'agents'                         
);

$posts = new WP_Query($args);

if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post(); ?>

    <wordpress post here>

<?php

endwhile; 

wp_reset_postdata();

endif;

?>

在页面的一些方面,我们得到由于第一个循环而无效的循环:

$args = array(
    'post_type' => 'properties',
    'posts_per_page' => -1,
    'property_types' => $page,
    'property_cities' => $urlCity,
    'meta_key' => 'select-agent-value',
    'meta_value' => $agentId,
    's' => $urlAddress                              
);

$posts = new WP_Query($args);

if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post();

    <wordpress post here>

endwhile; 

wp_reset_postdata();

endif; 

第二个循环中的参数来自此代码之前运行的代码:

$page = $_GET['property-type'];
$urlCity = $_GET['city'];
$urlAddress = $_GET['address'];
$agentId = $_GET['agentId'];

if ($page == 'all') {
    $page = '';
}                           

if ($urlCity == 'all') {
    $urlCity = '';
}

if ($agentId == 'all') {
    $agentId = '';
}

我觉得它可能很简单,我很想念。感谢任何人都能给予的帮助!

1 个答案:

答案 0 :(得分:0)

好的,所以我想出了问题,它最终与重置查询无关,就像我最初的感觉一样。发生了什么是在第二个循环中,我设置'property_types'=&gt; $ page,该变量值来自页面前面使用的$ _GET。无论出于何种原因,第一个循环是获取该变量并更改其值,最终影响第二个循环。所以我基本上只是在第一个循环之后重新编写了$ _GET,这确保了正确的值存储在变量中。

感谢所有可能一直在关注它并试图找出如何提供帮助的人。我有点盲目支持实际问题,最终使我很难提供帮助,因为我实际上并不知道实际发生了什么。