dqhnp44220 2016-11-16 23:33
浏览 66
已采纳

Wordpress多个wp_querys,wp_reset_postdata()不起作用

I'm trying to run two loops on a page. After the first loop runs, it completely stops the second loop. I looked into it and read up on wp_reset_postdata() and wp_reset_query(), neither of which have helped me so far.

My first loop looks like this:

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

?>

A little ways down the page, we get the loop that isn't working due to the first loop:

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

The arguments in the second loop are from this code that runs a little before it:

$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 = '';
}

I get the feeling it could something simple I'm missing. Appreciate any help anyone can give!

  • 写回答

1条回答 默认 最新

  • doulong1987 2016-11-17 21:25
    关注

    Okay so I figured out the issue, it ultimately had nothing to do with resetting the query as I originally felt. What was happening was in the second loop, where I set 'property_types' => $page, that variable value was from a $_GET used earlier on the page. For whatever reason, the first loop was taking that variable and changing its value, which eventually effected the second loop. So I basically just redid the $_GET after the first loop, which made sure the right value was stored in the variable.

    Thanks for anyone who might have been looking over it and trying to figure out how to help. I was kinda blind sided by what the actual problem was which ultimately made it hard to help since I actually had no clue what was actually happening.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况