\WP_Stager_Integration\Event::get_unfinished_events(int $limit = -1): array[]

Description

Gets events that have not ended yet, ordered by start ascending.

Selects on the program end time rather than the start time, so an event that is running right now still counts as upcoming, while an event that finished earlier today does not. Events without a usable end timestamp fall back to their start time.

Parameters:

ParameterTypeDescription
$limit int (Optional) The maximum number of events to return. -1 for no limit. Default value: -1.

Returns:

array[] List of raw Stager event arrays.

Information

Fileclass-event.php line 644

Full Code

/**
 * Gets events that have not ended yet, ordered by start ascending.
 *
 * Selects on the program end time rather than the start time, so an event that is running
 * right now still counts as upcoming, while an event that finished earlier today does not.
 * Events without a usable end timestamp fall back to their start time.
 *
 * @param int $limit (Optional) The maximum number of events to return. -1 for no limit. Default value: -1.
 *
 * @return array[] List of raw Stager event arrays.
 */
public static function get_unfinished_events( $limit = -1 ) {

    global $wpdb;

    // Type casting
    $limit = (int) $limit;

    // Get data
    $now       = time();
    $limit_sql = $limit > 0 ? $wpdb->prepare( "LIMIT %d", $limit ) : '';

    $sql = "
        SELECT pm_raw.meta_value
        FROM {$wpdb->posts} p
        JOIN {$wpdb->postmeta} pm_start
          ON p.ID = pm_start.post_id
         AND pm_start.meta_key = %s
        JOIN {$wpdb->postmeta} pm_raw
          ON p.ID = pm_raw.post_id
         AND pm_raw.meta_key = %s
        LEFT JOIN {$wpdb->postmeta} pm_end
          ON p.ID = pm_end.post_id
         AND pm_end.meta_key = %s
        WHERE p.post_type = %s
          AND p.post_status = 'publish'
          AND CAST( COALESCE( NULLIF( pm_end.meta_value, '' ), pm_start.meta_value ) AS UNSIGNED ) >= %d
        ORDER BY CAST(pm_start.meta_value AS UNSIGNED) ASC
        {$limit_sql}
    ";

    $rows = $wpdb->get_col( $wpdb->prepare(
                    $sql,
                    self::get_meta_key( self::META_KEY_TIMESTAMP_PROGRAM_START ),
                    self::get_meta_key( self::META_KEY_RAW_DATA ),
                    self::get_meta_key( self::META_KEY_TIMESTAMP_PROGRAM_END ),
                    self::CPT_SLUG,
                    $now
            ) );

    // Get events
    $events = (array) (array_map( 'maybe_unserialize', $rows ) ?: array());

    return self::parse_events( $events );
}

💡 If you ever get stuck or have a question, please check our FAQs, our Free Integration Service, our paid Full Integration Service, or reach out to us!

Get WP Stager Integration

🎁 Limited offer: Use code WELCOME26 to get your first month for free!