admin管理员组文章数量:1130349
I am using this function which seems to work fine, however I was wondering if I must use wp_reset_postdata after endwhile?
function evecal_task_function() {
$args=array(
'posts_per_page' => -1,
'post_type' => 'events'
);
$now = time();
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
$fxdate = strtotime(get_field('fl_date',false,false));$datediff = $now - $fxdate;
if ( $datediff > 1) {update_field('fl_expire', '1', get_the_ID());}
endwhile;
endif;
}
I am using this function which seems to work fine, however I was wondering if I must use wp_reset_postdata after endwhile?
function evecal_task_function() {
$args=array(
'posts_per_page' => -1,
'post_type' => 'events'
);
$now = time();
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
$fxdate = strtotime(get_field('fl_date',false,false));$datediff = $now - $fxdate;
if ( $datediff > 1) {update_field('fl_expire', '1', get_the_ID());}
endwhile;
endif;
}
Share
Improve this question
asked Oct 23, 2018 at 10:38
JoaMikaJoaMika
6986 gold badges27 silver badges58 bronze badges
1 Answer
Reset to default 2Yes you should, otherwise you could interfere with another query happening on the same page.
Using $the_query->the_post(); interferes with the global $post variable, and wp_reset_postdata() serves that exact purpose, to reset the global $post variable to the original (global) query. Read more
I am using this function which seems to work fine, however I was wondering if I must use wp_reset_postdata after endwhile?
function evecal_task_function() {
$args=array(
'posts_per_page' => -1,
'post_type' => 'events'
);
$now = time();
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
$fxdate = strtotime(get_field('fl_date',false,false));$datediff = $now - $fxdate;
if ( $datediff > 1) {update_field('fl_expire', '1', get_the_ID());}
endwhile;
endif;
}
I am using this function which seems to work fine, however I was wondering if I must use wp_reset_postdata after endwhile?
function evecal_task_function() {
$args=array(
'posts_per_page' => -1,
'post_type' => 'events'
);
$now = time();
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
$fxdate = strtotime(get_field('fl_date',false,false));$datediff = $now - $fxdate;
if ( $datediff > 1) {update_field('fl_expire', '1', get_the_ID());}
endwhile;
endif;
}
Share
Improve this question
asked Oct 23, 2018 at 10:38
JoaMikaJoaMika
6986 gold badges27 silver badges58 bronze badges
1 Answer
Reset to default 2Yes you should, otherwise you could interfere with another query happening on the same page.
Using $the_query->the_post(); interferes with the global $post variable, and wp_reset_postdata() serves that exact purpose, to reset the global $post variable to the original (global) query. Read more
本文标签: Do I need to use wpresetpostdata for my function
版权声明:本文标题:Do I need to use wp_reset_postdata for my function? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749238917a2337748.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论