0
votes

I'm trying to have posts automatically expire based on the role of the author of the post. While there are a lot of wordpress expiration plugins out there, the majority of them are granular in the sense that they let you set the expiration on a post by post basis. I was able to find one plugin (over 2 years old) which supposedly worked by user role and custom post type, but it was no longer working correctly. Ideally, what I need to accomplish is:

  • I have a CPT called animals (this is a lost/found board for local animal control)
  • Users can post (via the frontend), and have their own roles assigned (subscribers)
  • Animal service officers can also post (with logged in accounts) which all have editor roles assigned to them

Officers are only posting 'found' pets, and those posts must expire after 5 days (because they then head to adoption). Posts by all other users expire after 30 days.

Thoughts? I'm stuck.

1
what's supposed to happen to the expired posts? Get deleted? - Loai Nagati
They post status should change to draft (client wants them all kept for archival purposes) - user3412330

1 Answers

0
votes

1) Create Rol https://gist.github.com/loorlab/0a0ef9a9768cef098761

2) Insert into functions.php

     function auto_expire_posts(){
        global $wpdb, $wp_roles;
          //get all post ids of published posts.
        $post_ids = $wpdb->get_results( "SELECT ID FROM $wpdb->posts WHERE post_status ='publish' " );

foreach($post_ids as $id){
   $postid =  $id->ID;

    //get the post publish date
    $origpostdate = get_the_date($d, $the_post->post_parent);

    if ($_GET['role'] == "officers" ) //I don't if I do it right here..
        {

    $expiration_value = strtotime ( '+1 day' , strtotime ( $origpostdate) );// adding 30 days from the publish date for role = author

      if($expiration_value){

       $todays_date = date("Y-m-d"); 

       $today = strtotime($todays_date); 

       $expiration_date = $expiration_value;

       if ($expiration_date > $today) { 

              //do not do anything

              } else { 

      // it is expired, we set post status to trash, without changing anything 

            $my_post = array();

            $my_post['ID'] = $postid;

            $my_post['post_status'] = 'trash';

            // Update the post into the database

             wp_update_post( $my_post );

           }

      }//end if(expiration_value);

    }
    elseif ($_GET['role'] == "officers" ){

       $expiration_value = strtotime ( '+1 day' , strtotime ( $origpostdate) ); //adding 30 days from the publish date for role = contributor

       if($expiration_value){

       $todays_date = date("Y-m-d"); 

       $today = strtotime($todays_date); 

       $expiration_date = $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'] = 'trash';

            // Update the post into the database

             wp_update_post( $my_post );

            }

      }//end if(expiration_value);

  }
  else {}//do nothing here

 }
 }

//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(), 'hourly', 'auto_expire_posts_cron_hook' );
 }

 add_action('auto_expire_posts_cron_hook','auto_expire_posts');