Skip to content

Instantly share code, notes, and snippets.

@bhays
Created May 22, 2019 21:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bhays/7db8adf2137074082316b36d990ac9e6 to your computer and use it in GitHub Desktop.
Save bhays/7db8adf2137074082316b36d990ac9e6 to your computer and use it in GitHub Desktop.
WordPress plugin that will set Tribe Events Calendar events to draft status 1 day after event ends.
<?php
/**
* Plugin Name: TEC Demote Events
* Plugin URI:
* Description: Set expired events as drafts one day after end date.
* Version: 1.0.0
* Author: Ben Hays
* Author URI: http://benjaminhays.com
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: brh-tec-demote
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
define( 'BRH_TEC_DEMOTE_VERSION', '1.0.0' );
/**
* The code that runs during plugin activation.
*/
function activate_brh_tec_demote() {
if( !wp_next_scheduled( 'tec_demote' ) ) {
wp_schedule_event( time(), 'daily', 'tec_demote' );
}
}
/**
* The code that runs during plugin deactivation.
*/
function deactivate_brh_tec_demote() {
$timestamp = wp_next_scheduled( 'tec_demote' );
wp_unschedule_event( $timestamp, 'tec_demote' );
}
register_activation_hook( __FILE__, 'activate_brh_tec_demote' );
register_deactivation_hook( __FILE__, 'deactivate_brh_tec_demote' );
// here's the function we'd like to call with our cron job
function tec_demote_events() {
// Get all published events that have past
$yesterday = strtotime('yesterday 23:59:59');
$args = array(
'post_status' => 'publish',
'post_type' => 'tribe_events',
'posts_per_page' => 100,
'no_found_rows' => true,
'update_post_term_cache' => false,
'tribe_suppress_query_filters' => true,
'fields' => 'ids',
'meta_query' => array(
array(
'key' => '_EventEndDate',
'value' => $yesterday,
'compare' => '>='
),
),
);
$events = new WP_Query($args);
// Set status to draft
if( $events->have_posts() ){
foreach( $events['posts'] as $p ){
$query = array(
'ID' => $p,
'post_status' => 'draft',
);
wp_update_post( $query, true );
}
}
}
// hook that function onto our scheduled event:
add_action ('tec_demote', 'tec_demote_events');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment