\WP_Stager_Integration\Event::get_events_within_date_range(int|null $start = 0, int|null $end = 0, int $limit = -1): array[]

Description

Gets events within a date range (raw Stager data).

Parameters:

ParameterTypeDescription
$start int|null The start timestamp. Use null or 0 for no lower bound.
$end int|null The end timestamp. Use null or 0 for no upper bound.
$limit int Optional limit on number of events. -1 for no limit.

Returns:

array[] List of raw Stager event arrays.

Information

Fileclass-event.php line 450

Full Code

/**
 * Gets events within a date range (raw Stager data).
 *
 * @param int|null $start The start timestamp. Use null or 0 for no lower bound.
 * @param int|null $end   The end timestamp. Use null or 0 for no upper bound.
 * @param int      $limit Optional limit on number of events. -1 for no limit.
 *
 * @return array[] List of raw Stager event arrays.
 */
public static function get_events_within_date_range( $start = 0, $end = 0, $limit = -1 ) {

    global $wpdb;

    // Type casting
    $start = (int) $start;
    $end   = (int) $end;

    $clauses = array();
    $params  = array();

    if ( $start ) {
        $clauses[] = "CAST(pm_start.meta_value AS UNSIGNED) >= %d";
        $params[]  = $start;
    }
    if ( $end ) {
        $clauses[] = "CAST(pm_start.meta_value AS UNSIGNED) <= %d";
        $params[]  = $end;
    }

    $where     = $clauses ? "AND " . implode( " AND ", $clauses ) : '';
    $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
        WHERE p.post_type = %s
          AND p.post_status = 'publish'
          {$where}
        ORDER BY CAST(pm_start.meta_value AS UNSIGNED) ASC
        {$limit_sql}
    ";

    $params = array_merge(
            [ self::get_meta_key( self::META_KEY_TIMESTAMP_PROGRAM_START ),
                self::get_meta_key( self::META_KEY_RAW_DATA ),
                self::CPT_SLUG ],
            $params
    );

    $rows = $wpdb->get_col( $wpdb->prepare( $sql, $params ) );

    // 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!