Tribe__Events__Event_Cleaner_Scheduler::select_events_to_purge( int $month )

Selects events to be moved to trash or permanently deleted.


Parameters

$month

(int) (Required) - The value chosen by user to purge all events older than x months


Top ↑

Return

(array) $post_ids - an array of event Post_IDs with the Event End Date older than $month


Top ↑

Source

File: src/Tribe/Event_Cleaner_Scheduler.php

	public function select_events_to_purge( $month ) {
		/** @var wpdb $wpdb */
		global $wpdb;

		$event_post_type = Tribe__Events__Main::POSTTYPE;

		$posts_with_parents_sql = "SELECT DISTINCT post_parent
		FROM {$wpdb->posts}
		WHERE post_type= '$event_post_type'
			AND post_parent <> 0
		";

		$sql = "SELECT post_id
		FROM {$wpdb->posts} AS t1
		INNER JOIN {$wpdb->postmeta} AS t2 ON t1.ID = t2.post_id
		WHERE t1.post_type = %d
			AND t2.meta_key = '_EventEndDate'
			AND t2.meta_value <= DATE_SUB( CURDATE(), INTERVAL %d MONTH )
			AND t1.post_parent = 0
			AND t1.ID NOT IN ( $posts_with_parents_sql )
		";

		/**
		 * Filter - Allows users to manipulate the cleanup query
		 *
		 * @param string $sql - The query statement
		 *
		 * @since 4.6.13
		 */
		$sql = apply_filters( 'tribe_events_delete_old_events_sql', $sql );

		$args = array(
			'post_type' => $event_post_type,
			'date'      => $month,
		);

		/**
		 * Filter - Allows users to modify the query's placeholders
		 *
		 * @param array $args - The array of variables
		 *
		 * @since TDB
		 */
		$args = apply_filters( 'tribe_events_delete_old_events_sql_args', $args );

		/**
		 * Returns an array of Post IDs (events) that ended before a specific date
		 */
		$post_ids = $wpdb->get_col( $wpdb->prepare( $sql, $args ) );

		return $post_ids;
	}

Top ↑

Changelog

Changelog
Version Description
6.0.13 Now batches each purge. By default, it limits to 15 occurrences.
4.6.13 Introduced.