Advertisement
Mactonex

auto change post status

Jun 13th, 2012
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.23 KB | None | 0 0
  1. function auto_expire_posts(){
  2.  
  3. global $wpdb;
  4.  
  5. //get all post ids of published posts.
  6.  
  7. $post_ids = $wpdb->get_results( "SELECT ID FROM $wpdb->posts WHERE post_type ='event' AND post_status ='publish' " );
  8.  
  9.         foreach($post_ids as $id){
  10.  
  11.        $postid =  $id->ID;
  12.  
  13.        //get post meta value from post meta key expiration
  14.  
  15.        $expiration_value = get_post_meta($postid,'start_date',true);
  16.  
  17.        if($expiration_value){
  18.  
  19.                $todays_date = date("d-m-Y");
  20.  
  21.                $today = strtotime($todays_date);
  22.  
  23.                $expiration_date = strtotime($expiration_value);
  24.  
  25.                if ($expiration_date > $today) {
  26.  
  27.                           //do not do anything
  28.  
  29.                           } else {
  30.  
  31.                               // it is expired, we set post status to draft, without changing anything
  32.  
  33.                                 $my_post = array();
  34.  
  35.                                 $my_post['ID'] = $postid;
  36.  
  37.                             $my_post['post_status'] = 'private';
  38.  
  39.  
  40.                                 // Update the post into the database
  41.  
  42.                                  wp_update_post( $my_post );
  43.  
  44.                        }
  45.  
  46.                        
  47.  
  48.              }//end if(expiration_value);
  49.  
  50.         }
  51.  
  52. }
  53.  
  54.  
  55.  
  56. //verify event has not been scheduled
  57.  
  58.         if ( !wp_next_scheduled( 'auto_expire_posts_cron_hook' ) ) {
  59.  
  60.        
  61.  
  62.                 //schedule the event to run daily
  63.  
  64.                 wp_schedule_event( time(), 'daily', 'auto_expire_posts_cron_hook' );
  65.  
  66.                
  67.  
  68. }
  69.  
  70. add_action('auto_expire_posts_cron_hook','auto_expire_posts');
  71.  
  72.  
  73. //create menu to see cron hook schedules.
  74.  
  75. add_action('admin_menu','auto_exp_view_cron_menu');
  76.  
  77.  
  78. function auto_exp_view_cron_menu() {
  79.  
  80.  
  81.         //create view cron jobs settings page
  82.  
  83.     add_options_page( 'View Cron Jobs', 'View Cron Jobs', 'manage_options', 'boj-view-cron', 'exp_view_cron_settings' );
  84.  
  85.        
  86.  
  87. }
  88.  
  89.  
  90. function exp_view_cron_settings() {
  91.  
  92.  
  93.         $cron = _get_cron_array();
  94.  
  95.         $schedules = wp_get_schedules();
  96.  
  97.         $date_format = 'M j, Y @ G:i';
  98.  
  99.         ?>
  100.  
  101.         <div class="wrap" id="cron-gui">
  102.  
  103.         <h2>Cron Events Scheduled</h2>
  104.  
  105.    
  106.  
  107.         <table class="widefat fixed">
  108.  
  109.                 <thead>
  110.  
  111.                         <tr>
  112.  
  113.                                 <th scope="col">Next Run (GMT/UTC)</th>
  114.  
  115.                                 <th scope="col">Schedule</th>
  116.  
  117.                                 <th scope="col">Hook Name</th>
  118.  
  119.                         </tr>
  120.  
  121.                 </thead>
  122.  
  123.                 <tbody>
  124.  
  125.                         <?php foreach ( $cron as $timestamp => $cronhooks ) { ?>
  126.  
  127.                                 <?php foreach ( (array) $cronhooks as $hook => $events ) { ?>
  128.  
  129.                                         <?php foreach ( (array) $events as $event ) { ?>
  130.  
  131.                                                 <tr>
  132.  
  133.                                                         <td>
  134.  
  135.                                                                 <?php echo date_i18n( $date_format, wp_next_scheduled( $hook ) ); ?>
  136.  
  137.                             </td>
  138.  
  139.                                                         <td>
  140.  
  141.                                                                 <?php
  142.  
  143.                                                                 if ( $event[ 'schedule' ] ) {
  144.  
  145.                                                                         echo $schedules[ $event[ 'schedule' ] ][ 'display' ];
  146.  
  147.                                                                 } else {
  148.  
  149.                                                                         ?>One-time<?php
  150.  
  151.                                                                 }
  152.  
  153.                                                                 ?>
  154.  
  155.                                                         </td>
  156.  
  157.                                                         <td><?php echo $hook; ?></td>
  158.  
  159.                                                 </tr>
  160.  
  161.                                         <?php } ?>
  162.  
  163.                                 <?php } ?>
  164.  
  165.                         <?php } ?>
  166.  
  167.                 </tbody>
  168.  
  169.         </table>
  170.  
  171.     </div>
  172.  
  173. <?
  174.  
  175. }
  176.  
  177. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement