admin管理员组文章数量:1130349
Edit - I already tried wp_reset_query() it doesn't work, the first loop executes as intended but the second loop just jumps to the else and I get not working
I am using 2 custom WP_Query loops in one page, the first is to get posts from a certain category, and the second is just getting the posts by dates,
here's the code
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'any',
'category' => 3,
'posts_per_page' => 4);
$query_var= new WP_Query($args);
if ($query_var->have_posts()):
while ($query_var->have_posts()):
$query_var->the_post(); ?>
<div class="patta p-4 col-lg-3 col-md-6">
<!-- FIX THIS -->
<img class="card-img-top"
src="<?php the_post_thumbnail(); ?>"
alt="<?php the_post_thumbnail_caption() ?>"/>
<h4><b><?php the_title(); ?></b><br></h4>
<p>
<a href="#">Link</a>
</p>
</div>
<?php
endwhile;
wp_reset_postdata();
endif;
?>
second loop
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'any',
'orderby' => 'post_date',
'order' => 'DESC',
'posts_per_page' => 4);
$query_var= new WP_Query($args);
if ($query_var->have_posts()):
while ($query_var->have_posts()):
$query_var->the_post(); ?>
<div class="patta p-4 col-lg-3 col-md-6">
<!-- FIX THIS -->
<img class="card-img-top"
src="<?php if (the_post_thumbnail()): the_post_thumbnail(); else:echo '.png';endif; ?>"
alt="<?php the_post_thumbnail_caption() ?>"/>
<h4><b><?php the_title(); ?></b><br></h4>
<p>
<a href="#">Link</a>
</p>
</div>
<?php
endwhile;
wp_reset_postdata();
else: echo 'not working';
endif;
?>
I am using wp_reset_postdata(); but it doesn't seem to be working.
Edit - I already tried wp_reset_query() it doesn't work, the first loop executes as intended but the second loop just jumps to the else and I get not working
I am using 2 custom WP_Query loops in one page, the first is to get posts from a certain category, and the second is just getting the posts by dates,
here's the code
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'any',
'category' => 3,
'posts_per_page' => 4);
$query_var= new WP_Query($args);
if ($query_var->have_posts()):
while ($query_var->have_posts()):
$query_var->the_post(); ?>
<div class="patta p-4 col-lg-3 col-md-6">
<!-- FIX THIS -->
<img class="card-img-top"
src="<?php the_post_thumbnail(); ?>"
alt="<?php the_post_thumbnail_caption() ?>"/>
<h4><b><?php the_title(); ?></b><br></h4>
<p>
<a href="#">Link</a>
</p>
</div>
<?php
endwhile;
wp_reset_postdata();
endif;
?>
second loop
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'any',
'orderby' => 'post_date',
'order' => 'DESC',
'posts_per_page' => 4);
$query_var= new WP_Query($args);
if ($query_var->have_posts()):
while ($query_var->have_posts()):
$query_var->the_post(); ?>
<div class="patta p-4 col-lg-3 col-md-6">
<!-- FIX THIS -->
<img class="card-img-top"
src="<?php if (the_post_thumbnail()): the_post_thumbnail(); else:echo 'https://lamasec.pythonanywhere/static/img/vulnhub.png';endif; ?>"
alt="<?php the_post_thumbnail_caption() ?>"/>
<h4><b><?php the_title(); ?></b><br></h4>
<p>
<a href="#">Link</a>
</p>
</div>
<?php
endwhile;
wp_reset_postdata();
else: echo 'not working';
endif;
?>
I am using wp_reset_postdata(); but it doesn't seem to be working.
- please try : wp_reset_query(); – vikrant zilpe Commented Dec 24, 2018 at 9:18
- also try this code : $wpdb->reset_postdata() – vikrant zilpe Commented Dec 24, 2018 at 9:27
- Both of them don't work. @vikrantzilpe – Sahil Shukla Commented Dec 24, 2018 at 9:29
- I don’t see how that function is related to your problem – Milo Commented Dec 24, 2018 at 9:36
- you set it up again wordpress environment ? – vikrant zilpe Commented Dec 24, 2018 at 10:30
1 Answer
Reset to default 0After reading the documentation for multiple loops in one page here, I only had one WP_Query for both the loops. I stored the ID's of all the posts from the desired category and check for them in the second loop and continue over them. Here's the final code -
first loop
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'any',
'category' => 3,
'posts_per_page' => 4);
$query_var = new WP_Query($args);
if ($query_var->have_posts()):
while ($query_var->have_posts()):
$query_var->the_post();
$do_not_duplicate[] = $post->ID; ?>
<div class="patta p-4 col-lg-3 col-md-6">
<!-- FIX THIS -->
<img class="card-img-top"
src="<?php the_post_thumbnail(); ?>"
alt="<?php the_post_thumbnail_caption() ?>"/>
<h4><b><?php the_title(); ?></b><br></h4>
<p>
<a href="#">Link</a>
</p>
</div>
<?php endwhile; ?>
Second Loop
if (have_posts()):
while (have_posts()):
the_post();
if (in_array($post->ID, $do_not_duplicate)) continue;
?>
<div class="patta p-4 col-lg-3 col-md-6">
<!-- FIX THIS -->
<img class="card-img-top"
src="<?php the_post_thumbnail(); ?>"
alt="<?php the_post_thumbnail_caption() ?>"/>
<h4><b><?php the_title(); ?></b><br></h4>
<p>
<a href="#">Link</a>
</p>
</div>
<?php
endwhile;
endif;
endif;
?>
Edit - I already tried wp_reset_query() it doesn't work, the first loop executes as intended but the second loop just jumps to the else and I get not working
I am using 2 custom WP_Query loops in one page, the first is to get posts from a certain category, and the second is just getting the posts by dates,
here's the code
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'any',
'category' => 3,
'posts_per_page' => 4);
$query_var= new WP_Query($args);
if ($query_var->have_posts()):
while ($query_var->have_posts()):
$query_var->the_post(); ?>
<div class="patta p-4 col-lg-3 col-md-6">
<!-- FIX THIS -->
<img class="card-img-top"
src="<?php the_post_thumbnail(); ?>"
alt="<?php the_post_thumbnail_caption() ?>"/>
<h4><b><?php the_title(); ?></b><br></h4>
<p>
<a href="#">Link</a>
</p>
</div>
<?php
endwhile;
wp_reset_postdata();
endif;
?>
second loop
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'any',
'orderby' => 'post_date',
'order' => 'DESC',
'posts_per_page' => 4);
$query_var= new WP_Query($args);
if ($query_var->have_posts()):
while ($query_var->have_posts()):
$query_var->the_post(); ?>
<div class="patta p-4 col-lg-3 col-md-6">
<!-- FIX THIS -->
<img class="card-img-top"
src="<?php if (the_post_thumbnail()): the_post_thumbnail(); else:echo '.png';endif; ?>"
alt="<?php the_post_thumbnail_caption() ?>"/>
<h4><b><?php the_title(); ?></b><br></h4>
<p>
<a href="#">Link</a>
</p>
</div>
<?php
endwhile;
wp_reset_postdata();
else: echo 'not working';
endif;
?>
I am using wp_reset_postdata(); but it doesn't seem to be working.
Edit - I already tried wp_reset_query() it doesn't work, the first loop executes as intended but the second loop just jumps to the else and I get not working
I am using 2 custom WP_Query loops in one page, the first is to get posts from a certain category, and the second is just getting the posts by dates,
here's the code
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'any',
'category' => 3,
'posts_per_page' => 4);
$query_var= new WP_Query($args);
if ($query_var->have_posts()):
while ($query_var->have_posts()):
$query_var->the_post(); ?>
<div class="patta p-4 col-lg-3 col-md-6">
<!-- FIX THIS -->
<img class="card-img-top"
src="<?php the_post_thumbnail(); ?>"
alt="<?php the_post_thumbnail_caption() ?>"/>
<h4><b><?php the_title(); ?></b><br></h4>
<p>
<a href="#">Link</a>
</p>
</div>
<?php
endwhile;
wp_reset_postdata();
endif;
?>
second loop
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'any',
'orderby' => 'post_date',
'order' => 'DESC',
'posts_per_page' => 4);
$query_var= new WP_Query($args);
if ($query_var->have_posts()):
while ($query_var->have_posts()):
$query_var->the_post(); ?>
<div class="patta p-4 col-lg-3 col-md-6">
<!-- FIX THIS -->
<img class="card-img-top"
src="<?php if (the_post_thumbnail()): the_post_thumbnail(); else:echo 'https://lamasec.pythonanywhere/static/img/vulnhub.png';endif; ?>"
alt="<?php the_post_thumbnail_caption() ?>"/>
<h4><b><?php the_title(); ?></b><br></h4>
<p>
<a href="#">Link</a>
</p>
</div>
<?php
endwhile;
wp_reset_postdata();
else: echo 'not working';
endif;
?>
I am using wp_reset_postdata(); but it doesn't seem to be working.
- please try : wp_reset_query(); – vikrant zilpe Commented Dec 24, 2018 at 9:18
- also try this code : $wpdb->reset_postdata() – vikrant zilpe Commented Dec 24, 2018 at 9:27
- Both of them don't work. @vikrantzilpe – Sahil Shukla Commented Dec 24, 2018 at 9:29
- I don’t see how that function is related to your problem – Milo Commented Dec 24, 2018 at 9:36
- you set it up again wordpress environment ? – vikrant zilpe Commented Dec 24, 2018 at 10:30
1 Answer
Reset to default 0After reading the documentation for multiple loops in one page here, I only had one WP_Query for both the loops. I stored the ID's of all the posts from the desired category and check for them in the second loop and continue over them. Here's the final code -
first loop
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'any',
'category' => 3,
'posts_per_page' => 4);
$query_var = new WP_Query($args);
if ($query_var->have_posts()):
while ($query_var->have_posts()):
$query_var->the_post();
$do_not_duplicate[] = $post->ID; ?>
<div class="patta p-4 col-lg-3 col-md-6">
<!-- FIX THIS -->
<img class="card-img-top"
src="<?php the_post_thumbnail(); ?>"
alt="<?php the_post_thumbnail_caption() ?>"/>
<h4><b><?php the_title(); ?></b><br></h4>
<p>
<a href="#">Link</a>
</p>
</div>
<?php endwhile; ?>
Second Loop
if (have_posts()):
while (have_posts()):
the_post();
if (in_array($post->ID, $do_not_duplicate)) continue;
?>
<div class="patta p-4 col-lg-3 col-md-6">
<!-- FIX THIS -->
<img class="card-img-top"
src="<?php the_post_thumbnail(); ?>"
alt="<?php the_post_thumbnail_caption() ?>"/>
<h4><b><?php the_title(); ?></b><br></h4>
<p>
<a href="#">Link</a>
</p>
</div>
<?php
endwhile;
endif;
endif;
?>
本文标签: Unable to reset post data in wordpress custom query
版权声明:本文标题:Unable to reset post data in wordpress custom query 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749070906a2311559.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论