简体   繁体   中英

Infinite loop in wordpress WP_Query loop

I have the following code, i'm not sure having context of what happens within the while loop is hugely important. The issue lies with the continue; in the if statement. The condition in the if is met, and so continue; is reached. this created an infinite loop though. I can not understand why this is the case though?

Could anyone suggest why WordPress can not handle a continue; in a WP_Query loop?

while ($latest_events->have_posts()) {

        $id = get_the_ID();             
        $custom_fields = base_get_all_custom_fields($id);           
        $event_type = $custom_fields["course/event_type"][0];

        if(isset($event_type) && strpos_arr($event_type, array('Training')) !== false ){
                continue;
        }  

        $latest_events->the_post();
        get_template_part('partials/latest-espresso-events');   
 }

If you don't call $latest_event->the_post() the loop counter will not advanced, therefor you will have an infinite loop.

You must call $latest_event->the_post() before the continue; statement to make sure you go to the next post (otherwise the $latest_events->have_posts() will always return TRUE ).

        if(isset($event_type) && strpos_arr($event_type, array('Training')) !== false ){
            $latest_event->the_post();
            continue;
        }  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM