WordPress:25天后发送电子邮件给帖子的作者

时间:2015-10-01 05:57:17

标签: php wordpress

我需要在30天之后将自定义帖子类型banner中的任何帖子转换为草稿,我通过以下功能(操作)执行此操作,但是我需要在25天后发布此帖子发送电子邮件给他那告诉他我们转发到草稿的帖子。

怎么做?

function ads_show_expire(){
  global $wpdb;
  $result = $wpdb->get_results("SELECT * FROM wp_posts WHERE post_type = 'banner' AND post_status = 'publish'");
  if( !empty($result)) foreach ($result as $a){
      $show_time = get_the_time("Y-m-d", $a->ID );
      $show_time=date("Y-m-d", strtotime("+30 day", strtotime($show_time)));
      if ( $show_time < date("Y-m-d")){//compare 1 year after post with current time
          $my_post = array();
          $my_post['ID'] = $a->ID;
          $my_post['post_status'] = 'draft';
          wp_update_post( $my_post );
      }
  } // end foreach
}

add_action( 'init', 'ads_show_expire' );

2 个答案:

答案 0 :(得分:0)

您需要设置每天执行的cronjob并发送邮件并将该帖子转换为草稿。每当有任何用户访问该页面时,您编写的代码将被执行。因此,最好的解决方案是为此创建cron-job。

何时获得25天的条件 $ where。=&#34;和日期(post_date)=&#39;&#34; 。日期(&#39; Y-m-d&#39;,strtotime(&#39; -25天&#39;))。 &#34;&#39;&#34 ;;

 `if ( $show_time < date("Y-m-d")){//compare 1 year after post with current time
      $my_post = array();
      $my_post['ID'] = $a->ID;
      $my_post['post_status'] = 'draft';
      wp_update_post( $my_post );
      $user_info = get_userdata($a->post_author);
      $email = $user_info->user_email;
  }`

答案 1 :(得分:0)

它可能对你有帮助,

  • 您应该使用cpanel设置cron作业文件,因为您必须编写上面的代码,并且还要获取发布日期并将该日期与当前日期进行比较,它是匹配比编写发送邮件的电子邮件功能告诉他的那篇文章我们转换为草稿。 下面是cron文件的示例代码,您应该将其放在wordpress文件夹的根文件夹中供您参考:

<?php // FOR DATABASE OPERATION include('../wp-load.php'); // FOR GET_USERDATA FUNCTION & $WPDB include('../wp-includes/pluggable.php'); wp-includes/author-template.php //sample email code $headers = "MIME-Version: 1.0" . "\r\n"; $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; $headers .= 'From: <test@gmail.com>' . "\r\n"; $headers .= 'Cc: test@gmail.com' . "\r\n"; $to = "test@gmail.com"; $subject = "subject text"; $meesage = "test"; $sentmail = mail($to,$subject,$message,$headers); if($sentmail) echo $msg = "Mail has been sent successfully..."; else echo $msg = "Mail has been failed..."; 在此之前,你应该使用上面的功能代码。

您可以使用以下功能获取作者电子邮件:

<?php the_author_email(); ?> 要么 <?php get_the_author_meta( 'user_email', $userID ); ?>