WordPress根据用户角色过期帖子

时间:2016-01-22 19:32:11

标签: php wordpress post

我试图根据帖子作者的角色自动过期。虽然有很多wordpress过期插件,但大多数都是粒度的,因为它们允许你逐个帖子地设置过期。我能够找到一个插件(超过2年),据说可以按用户角色和自定义帖子类型工作,但它不再正常工作。理想情况下,我需要完成的是:

  • 我有一个名为动物的CPT(这是一个丢失/找到的本地板 动物控制)
  • 用户可以发布(通过前端),并分配自己的角色(订阅者)
  • 动物服务人员也可以发布(登录帐户) 所有人都有分配给他们的编辑角色

官员只发布'发现'宠物,这些帖子必须在5天后过期(因为他们然后前往收养)。所有其他用户的帖子将在30天后过期。

思考?我被卡住了。

1 个答案:

答案 0 :(得分:0)

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

2)插入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');