0

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');   
 }
4
  • are you trying to exit the loop at that point or is it deliberate that you create the infinite loop? Commented Jul 21, 2016 at 14:44
  • I'm trying to 'skip' the iteration and go to the next object Commented Jul 21, 2016 at 14:45
  • @Liam, did you check the solution I gave? Does it work? Commented Jul 22, 2016 at 16:17
  • @Dekel it does, apologies Commented Jul 28, 2016 at 17:28

1 Answer 1

1

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;
        }  
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.