• Resolved aledef

    (@aledef)


    Hi,
    let’s say there is an exibition that lasts for six months.
    It has started some time ago (no matter when), and it will end in the future (no matter when). This is what I mean with “ongoing event”.

    Well, there is no way of getting it using tribe_get_events() function so I wrote a query to get ongoing events in the correct order and pass them to WP_Query.

    This is the working code:

    global $wpdb;   
    
    $terms_string = $events_cats_separated_by_commas;
    $date = new DateTime();
    $date_string = $date->format('Y-m-d H:i:s');
    
    $querystr = "SELECT $wpdb->posts.ID,
    CAST( orderby_event_date_meta.meta_value AS DATETIME ) AS event_date,
    CAST( orderby_event_duration_meta.meta_value AS DECIMAL ) AS event_duration
    FROM $wpdb->posts
    LEFT JOIN $wpdb->term_relationships
    ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
    LEFT JOIN $wpdb->postmeta
    ON ($wpdb->posts.ID = $wpdb->postmeta.post_id
    AND $wpdb->postmeta.meta_key = '_EventHideFromUpcoming' )
    LEFT JOIN $wpdb->postmeta AS mt1
    ON ( $wpdb->posts.ID = mt1.post_id )
    LEFT JOIN $wpdb->postmeta AS orderby_event_date_meta
    ON ( orderby_event_date_meta.post_id = $wpdb->posts.ID
    AND orderby_event_date_meta.meta_key = '_EventStartDate' )
    LEFT JOIN $wpdb->postmeta AS orderby_event_duration_meta
    ON ( orderby_event_duration_meta.post_id = $wpdb->posts.ID
    AND orderby_event_duration_meta.meta_key = '_EventDuration' )
    WHERE 1=1
    AND ( $wpdb->term_relationships.term_taxonomy_id IN ($terms_string) )
    AND ( $wpdb->postmeta.post_id IS NULL
    AND ( mt1.meta_key = '_EventEndDate'
    AND CAST(mt1.meta_value AS DATETIME) > '$date_string' ) )
    GROUP BY $wpdb->posts.ID
    ORDER BY event_date ASC, event_duration ASC, $wpdb->posts.post_date ASC";
    
    $results = $wpdb->get_results($querystr, ARRAY_A);                   
    $post_ids = wp_list_pluck( $results, 'ID' );
    
    $query = new WP_Query( array(
        'post_type'         => Tribe__Events__Main::POSTTYPE,
        'post_status'       => 'publish',
        'post__in'          => $post_ids,
        'orderby'            => 'post__in',
        'order'                => 'ASC'
    ) );

    Hope it will help anybody who runs into my same problem.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hello @aledef

    Thanks for this!! We love it when our users make amazing contributions like this to the community.

    This will definitely be a very useful resource for others looking to expand the functionality of the plugin.

    Once again, thanks for your contribution.

    Best regards,

    Marho

    Thread Starter aledef

    (@aledef)

    Glad to be of help!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to get ongoing events and pass them to WP_Query’ is closed to new replies.