0

I am struggling to work out why the below code would cause an infinite loop, any pointers would be brilliant.

function send_reminder_email() {    
    $args = array(
        'post_type'     => 'product',
        'meta_key'      => 'WooCommerceEventsDate',
        'orderby'       => 'meta_value_num',
        'post_status' => 'publish',
      'order'           => 'ASC',       
        'meta_query'    => array(
                array(
                    'key'     => 'WooCommerceEventsDate',
                    'value'   => date('Y-m-d', strtotime('+7 days')),
                    'compare' => '=',
                ),
        ),
    );
    $query = new WP_Query( $args );

    if($query->have_posts()) : 
        while($query->have_posts()) : 
            $query->the_post();     

            $event[] = array( "title" => get_the_title(), "id" => get_the_ID());

        endwhile;           
    endif;
    wp_reset_query();

    print_r($event);


    foreach($event as $key => $value): 

        $args = array(
            'post_type'  => 'event_magic_tickets',
            'meta_key'   => 'WooCommerceEventsProductID',
            'post_status'=> 'publish',          
            'meta_query' => array(
                    array(
                        'key'     => 'WooCommerceEventsProductID',
                        'value'   => $value['id'],
                        'compare' => '=',
                    ),
            ),
        );

        $ticketQuery = new WP_Query( $args );       

        if($ticketQuery->have_posts()) : 

            while($ticketQuery->have_posts()): 
                echo "Do something here.";                      
            endwhile;   

        endif;
        wp_reset_query();
    endforeach;         

}
  • Amazing, thank you so much. Knew it was something simple. :) – Joe Jan 12 '17 at 16:23
1

Your second loop, the $ticketQuery loop is missing $ticketQuery->the_post().

The function the_post() actually sets up things like template tags, the $post object. The most important thing it does is increments the post counter so that eventually $ticketQuery->have_posts() will eventually return false. Without the_post() is your infinite loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.