• Resolved mfroml

    (@mhugg1961)


    From what I can tell, the earliest you can “Move to trash events older than/This option allows you to automatically move past events to trash” (on the General Settings page) is one month. I would like to move any event to trash immediately after it occurred. (So maybe rather than one month, one day?)

    Is this possible?

    Thanks

Viewing 7 replies - 1 through 7 (of 7 total)
  • hey, I′m here for the same thing!! I hope we can get an answer!

    Plugin Contributor Andras Guseo

    (@aguseo)

    Hi @mhugg1961 and @cenzi

    Currently, this is not possible out of the box or with snippets.

    Achieving something like this would require the modification of some core files, which modifications would be lost after a plugin update.

    The query needs to be changed here:
    wp-content/plugins/the-events-calendar/src/Tribe/Event_Cleaner_Scheduler.php

    In this function select_events_to_purge() look for this line:
    AND t2.meta_value <= DATE_SUB( CURDATE(), INTERVAL %d MONTH )

    and change it to:
    AND t2.meta_value <= DATE_SUB( CURRENT_TIMESTAMP(), INTERVAL %d MINUTE )

    With this change, the ‘1 month’ setting under “Events > Settings > General tab > Move to trash / Permanently delete events older than” will be taken as ‘1 minute’.
    (3 months as 3 minutes, etc.)

    Also you will need to change the related crons, as the trash and delete actions run on the cron.

    • tribe_del_event_cron
    • tribe_trash_event_cron

    Currently, these are set to run once per day. I would change them to run every 15 minutes. A plugin like WP Crontrol can help you do that.

    I would not set it to more often than 15 minutes, unless you have a lot of events to get rid of and a lot of visitors.
    With the 15-minute setting, the old events will be trashed or deleted every 15 minutes.

    Again, warning, if you make that change in the core file, the change will be lost after a plugin update and you will need to re-do it again.

    I also strongly recommend a full backup before attempting this.

    If this is something you would like to see as a feature, then I would recommend you to add it to our idea board, or upvote it if it’s already there.

    Let me know if this helps.

    Cheers,
    Andras

    Plugin Contributor Andras Guseo

    (@aguseo)

    Hi again @cenzi and @mhugg1961

    I was digging around a bit and I actually found a way to do this without needing to modify the core files. The is a filter that can be used for this.

    Here is the code you need to put in your funcitions.php file:

    add_filter( 'tribe_events_delete_old_events_sql', 'immediate_cleanup_query' );
    
    function immediate_cleanup_query( $sql ) {
    	global $wpdb;
    
    	$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 = %s
    				AND t2.meta_key = '_EventEndDate'
    				AND t2.meta_value <= DATE_SUB( CURRENT_TIMESTAMP(), INTERVAL %d MINUTE )
    				AND t2.meta_value != 0
    				AND t2.meta_value != ''
    				AND t2.meta_value IS NOT NULL
    				AND t1.post_parent = 0
    				AND t1.ID NOT IN ( $posts_with_parents_sql )
    		";
    
    	return $sql;
    }

    This will change the values in the setting dropdowns from months to minutes.
    So if you have “Trash events older than 1 month” selected, that will actually mean “Trash events older than 1 minute”. Same for permanently deleting events.

    Note: the texts in the dropdown will not change.

    There is one thing left to do.
    As I mentioned, this functionality runs on a cron that gets triggered once per day.

    If you install WP Crontrol then you can change that to run every 15 minutes.
    Go to Tools > Cron Events.
    Look for these two crons:

    • tribe_del_event_cron
    • tribe_trash_event_cron

    Edit them and set the recurrence to ‘Every 15 minutes’.

    You could set it to ‘Every minute’, but that would put superfluous load on your site.

    Cheers,
    Andras

    Plugin Contributor Andras Guseo

    (@aguseo)

    Good news @cenzi @mhugg1961

    I got really interested, so I created a free extension.
    Remove any code that I shared before and install this.
    https://theeventscalendar.com/extensions/remove-past-events/

    Would love to hear your feedback.

    Andras

    okay, awesome, you sir are amazing!! I’ll try this later tonight.

    Plugin Contributor Andras Guseo

    (@aguseo)

    @cenzi @mhugg1961
    Let me know how it worked out!

    A.

    Hi there!

    This thread has been inactive for a while so I’ll be marking it as Resolved.

    Please feel free to open a new thread if any other questions come up and we’d be happy to help.

    Best regards,

    Marho

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Move to trash events older than’ is closed to new replies.