Why does this foreach loop end early?

I’m wondering why the foreach loop in this function ends after outputting the data after it goes through the loop just once. I’ve run the query in MySQL and it returns the 8 records that I’m looking for. When I print_r the array that it all gets packed into before and after the foreach it shows 8 record sets in the array.

However, when I run the function on the page, it only outputs the first record set and then stops. Any ideas why that is?

Read More

Also, there’s a really long, stupid reason for cramming this all into a single function. Just know, it was my only option.

function query_hp_hero_events() {
global $wpdb;

$sql = "SELECT SQL_CALC_FOUND_ROWS *, mt2.meta_value AS start_time, mt3.meta_value AS end_time, mt4.meta_value AS start_date, mt5.meta_value AS event_id, mt6.meta_value AS event_meta, mt7.meta_value AS event_thumb FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) INNER JOIN wp_postmeta AS mt1 ON (wp_posts.ID = mt1.post_id) INNER JOIN wp_postmeta AS mt2 ON (wp_posts.ID = mt2.post_id AND mt2.meta_key = 'event_start_time') INNER JOIN wp_postmeta AS mt3 ON (wp_posts.ID = mt3.post_id AND mt3.meta_key = 'event_end_time') INNER JOIN wp_postmeta AS mt4 ON (wp_posts.ID = mt4.post_id AND mt4.meta_key = 'event_start_date') INNER JOIN wp_postmeta AS mt5 ON (wp_posts.ID = mt5.post_id AND mt5.meta_key = 'event_id') INNER JOIN wp_postmeta AS mt6 ON (wp_posts.ID = mt6.post_id AND mt6.meta_key = 'event_meta') INNER JOIN wp_postmeta AS mt7 ON (wp_posts.ID = mt7.post_id AND mt7.meta_key = 'event_thumbnail_url') WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (7) ) AND wp_posts.post_type = 'espresso_event' AND (wp_posts.post_status = 'publish') AND (wp_postmeta.meta_key = 'event_start_date' AND  (mt1.meta_key = 'event_start_date' AND CAST(mt1.meta_value AS DATE) >= '" . date('Y-m-d') . "') ) GROUP BY wp_posts.ID ORDER BY CAST(wp_postmeta.meta_value AS DATE) ASC, CAST(mt2.meta_value AS TIME) ASC LIMIT 0, 8";

echo $sql;

$events_query = $wpdb->get_results( $wpdb->prepare( $sql ));

foreach ($events_query as $event_listing) {

    $eventDate = strtotime($event_listing->start_date);
    $eventEndTime = strtotime($event_listing->end_time);
    $eventStartTime = strtotime($event_listing->start_time);
    $eventId = $event_listing->event_id;
    $eventMeta = $event_listing->event_meta;
    $eventThumb = $event_listing->event_thumb;

    $newEventMeta = unserialize($eventMeta);
    $newEventMeta2 = unserialize($newEventMeta);

    $homepageOverride = $newEventMeta2["homepage_override"];
    $ticketLink = $newEventMeta2["ticket_link"];
    $suiteRentals = $newEventMeta2["suite_rentals_link"];
    $vipTickets = $newEventMeta2["vip_ticket_link"];
    $groupTicketSales = $newEventMeta2["group_sales_link"];
    $parkingLink = $newEventMeta2["parking"];

    if ($eventDate >= strtotime(date('Y-m-d')) && $eventEndTime >= strtotime(date('G:i'))) {

        $hero = '<div id="event' . $eventId . '">';
        $hero .= '<div class="details">';
        if ( in_category( 'news' ) ) {
            $hero .= '<h1><a href="' . get_permalink() . '">' . $event_listing->post_title . '</a></h1>';
        } else {
        $hero .= '<h1><a href="/event-registration/?ee=' . $eventId . '">' . $event_listing->post_title . '</a></h1>';
        }

        if ( isset ( $homepageOverride ) && $homepageOverride != "" ) {
            $hero .= '<h2>' . $homepageOverride . '</h2>';
        } else {
            $hero .= homepage_event_date_range($eventId, $eventStartTime);
        }

        $hero .= '<ul>';
        if ( in_category( 'news' ) ) {
            $hero .= '<li><a href="' . get_permalink() . '">More Info</a></li>';
        } else {
            $hero .= '<li><a href="/event-registration/?ee=' . $eventId . '">More Info</a></li>';
        }
        if ( isset ( $ticketLink ) && $ticketLink != "" ) { $hero .= '<li><a href="' . $ticketLink . '" rel="external">Buy Tickets</a></li>'; }
        if ( isset ( $suiteRentals) && $suiteRentals != "" ) { $hero .= '<li><a href="' . $suiteRentals . '">Suite Rentals</a></li>'; }
        if ( isset ( $vipTickets) && $vipTickets != "" ) { $hero .= '<li><a href="' . $vipTickets . '">VIP Seats</a></li>'; }
        if ( isset ( $groupTicketSales) && $groupTicketSales != "" ) { $hero .= '<li><a href="' . $groupTicketSales . '">Group Tickets</a></li>'; }
        if ( isset ( $parkingLink) && $parkingLink != "" ) { $hero .= '<li><a href="' . $parkingLink . '" rel="external">Parking</a></li>'; }
        $hero .= '</ul>';

        $hero .= '</div>';

        if ( isset( $regexMatch[0] ) && $regexMatch[0] != "" && isset( $youtubeIdMatch[0] ) && $youtubeIdMatch[0] != "") {
            $hero .= '<div class="yt-video-player" id="videoplayerdiv1" src="http://www.youtube.com/v/' . $youtubeIdMatch[0] . '?enablejsapi=1&playerapiid=videoplayergen1&version=3"></div>';
        } else {
            $hero .= '<img src="' . $eventThumb . '" width="595" height="350" alt="" class="heroImg" />';
        }

        $hero .= '</div>';

    }

    return $hero;

}
}

Related posts

Leave a Reply

3 comments

  1. Because you are returning in the for loop. So foreach loops exits on each run.Remove the return and add results to variables and use them afterwards.DO not return in for loop