Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function auto_expire_posts(){
- global $wpdb;
- //get all post ids of published posts.
- $post_ids = $wpdb->get_results( "SELECT ID FROM $wpdb->posts WHERE post_type ='event' AND post_status ='publish' " );
- foreach($post_ids as $id){
- $postid = $id->ID;
- //get post meta value from post meta key expiration
- $expiration_value = get_post_meta($postid,'start_date',true);
- if($expiration_value){
- $todays_date = date("d-m-Y");
- $today = strtotime($todays_date);
- $expiration_date = strtotime($expiration_value);
- if ($expiration_date > $today) {
- //do not do anything
- } else {
- // it is expired, we set post status to draft, without changing anything
- $my_post = array();
- $my_post['ID'] = $postid;
- $my_post['post_status'] = 'private';
- // Update the post into the database
- wp_update_post( $my_post );
- }
- }//end if(expiration_value);
- }
- }
- //verify event has not been scheduled
- if ( !wp_next_scheduled( 'auto_expire_posts_cron_hook' ) ) {
- //schedule the event to run daily
- wp_schedule_event( time(), 'daily', 'auto_expire_posts_cron_hook' );
- }
- add_action('auto_expire_posts_cron_hook','auto_expire_posts');
- //create menu to see cron hook schedules.
- add_action('admin_menu','auto_exp_view_cron_menu');
- function auto_exp_view_cron_menu() {
- //create view cron jobs settings page
- add_options_page( 'View Cron Jobs', 'View Cron Jobs', 'manage_options', 'boj-view-cron', 'exp_view_cron_settings' );
- }
- function exp_view_cron_settings() {
- $cron = _get_cron_array();
- $schedules = wp_get_schedules();
- $date_format = 'M j, Y @ G:i';
- ?>
- <div class="wrap" id="cron-gui">
- <h2>Cron Events Scheduled</h2>
- <table class="widefat fixed">
- <thead>
- <tr>
- <th scope="col">Next Run (GMT/UTC)</th>
- <th scope="col">Schedule</th>
- <th scope="col">Hook Name</th>
- </tr>
- </thead>
- <tbody>
- <?php foreach ( $cron as $timestamp => $cronhooks ) { ?>
- <?php foreach ( (array) $cronhooks as $hook => $events ) { ?>
- <?php foreach ( (array) $events as $event ) { ?>
- <tr>
- <td>
- <?php echo date_i18n( $date_format, wp_next_scheduled( $hook ) ); ?>
- </td>
- <td>
- <?php
- if ( $event[ 'schedule' ] ) {
- echo $schedules[ $event[ 'schedule' ] ][ 'display' ];
- } else {
- ?>One-time<?php
- }
- ?>
- </td>
- <td><?php echo $hook; ?></td>
- </tr>
- <?php } ?>
- <?php } ?>
- <?php } ?>
- </tbody>
- </table>
- </div>
- <?
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement