@@ -388,6 +388,7 @@ function se_template_event_next_previous(): void {
388388 }
389389}
390390
391+
391392/**
392393 * Gets the next event based on a time stamp.
393394 *
@@ -431,13 +432,17 @@ function se_event_get_next_event( int $event_id, ?int $event_date_id = null ): ?
431432 ),
432433 ),
433434 );
435+
436+ // Ensure any events that are not published are not included in the query.
437+ $ args ['post__not_in ' ] = se_get_date_ids_for_non_published_events ();
438+
434439 // If we dont allow grouping, add the event id to parent not in.
435440 if ( ! $ allow_grouping ) {
436- $ args ['post__not_in ' ] = array_map (
437- function ( $ post ) {
438- return $ post [ ' id ' ];
439- },
440- se_event_get_event_dates ( $ event_id )
441+ $ args ['post__not_in ' ] = array_unique (
442+ array_merge (
443+ $ args [ ' post__not_in ' ],
444+ array_map ( fn ( array $ date ): int => $ date [ ' id ' ], se_event_get_event_dates ( $ event_id ) )
445+ )
441446 );
442447 }
443448
@@ -498,13 +503,17 @@ function se_event_get_previous_event( int $event_id, ?int $event_date_id = null
498503 ),
499504 ),
500505 );
506+
507+ // Ensure any events that are not published are not included in the query.
508+ $ args ['post__not_in ' ] = se_get_date_ids_for_non_published_events ();
509+
501510 // If we dont allow grouping, add the event id to parent not in.
502511 if ( ! $ allow_grouping ) {
503- $ args ['post__not_in ' ] = array_map (
504- function ( $ post ) {
505- return $ post [ ' id ' ];
506- },
507- se_event_get_event_dates ( $ event_id )
512+ $ args ['post__not_in ' ] = array_unique (
513+ array_merge (
514+ $ args [ ' post__not_in ' ],
515+ array_map ( fn ( array $ date ): int => $ date [ ' id ' ], se_event_get_event_dates ( $ event_id ) )
516+ )
508517 );
509518 }
510519
@@ -522,6 +531,45 @@ function ( $post ) {
522531 return $ previous_event ;
523532}
524533
534+ if ( ! function_exists ( 'se_get_date_ids_for_non_published_events ' ) ) {
535+
536+ /**
537+ * Return an array of all event dates, where the parent is not published.
538+ *
539+ * @since 2.0.4
540+ *
541+ * @return int[]
542+ */
543+ function se_get_date_ids_for_non_published_events () {
544+ static $ dates = null ;
545+ if ( is_array ( $ dates ) ) {
546+ return $ dates ;
547+ }
548+
549+ // Get all events that not published (draft or pending or private).
550+ $ args = array (
551+ 'post_type ' => SE_Event_Post_Type::$ post_type ,
552+ 'post_status ' => array_diff ( get_post_stati (), array ( 'publish ' ) ),
553+ 'posts_per_page ' => -1 ,
554+ 'fields ' => 'ids ' ,
555+ );
556+ $ draft_dates = get_posts ( $ args );
557+
558+ $ dates = array ();
559+
560+ foreach ( $ draft_dates as $ draft_date ) {
561+ // Get all dates for this event.
562+ $ event_dates = se_event_get_event_dates ( $ draft_date );
563+ if ( ! empty ( $ event_dates ) ) {
564+ foreach ( $ event_dates as $ date ) {
565+ $ dates [] = $ date ['id ' ];
566+ }
567+ }
568+ }
569+ return $ dates ;
570+ }
571+ }
572+
525573if ( ! function_exists ( 'se_expired_event_notice ' ) ) {
526574 /**
527575 * Output the expired event notice.
0 commit comments