当帖子发布12个月时,向管理员发送审查通知电子邮件

时间:2020-10-07 作者:Trevor Petrie

每当一篇帖子发布超过12个月时,我想向网站管理员发送一封自动评论电子邮件。

我尝试过使用以下代码,但似乎无法使其正常工作,或者可能是因为我不完全确定将其放置在何处或如何命名???

任何帮助都将不胜感激。

//send review email to admin
function post_review_notification() {
    $old_post = 60*60*24*365;
    $site_title = get_bloginfo( \'name\' );
    $to = get_option(\'admin_email\');
    $subject = .$site_title .\'- Please review\' .$post->post_title;
    $message = \'This post is over 12 months old, please update or retire \'."\\n".\'Post Title: \' .$post->post_title. "\\n"."Visit the post: " . get_permalink( $post->ID );
    
   if((date(\'U\')-get_the_time(\'U\')) > $old_post) {
        wp_mail( $to, $subject, $message );
      }
      else {
      }
}

1 个回复
SO网友:Ivan Shatsky

You ask a good question, where I would put it or how I would call it? I don\'t think you want to loop over all the posts everytime somebody visiting your site checking if any of them are more than one year old. Here is some thoughts about how this could be realized.

Assume you have a function to send a reminder about a post:

function send_reminder( $post_id ) {
    if ( get_post_status( $post_id ) == \'publish\' ) {
        $post_data = get_post( $post_id );
        // send a reminder about the post
    }
}

You can use a wp_schedule_single_event function of WP Cron to schedule notification in a year when a new post gets published:

add_action(\'publish_post\', \'add_reminder\');
function add_reminder( $post_id ) {
    wp_schedule_single_event( time() + 60*60*24*365, \'send_reminder\', array( $post_id ) );
}

However this action would run every time when a post gets any update. I don\'t know what is the best approach, read this Q/A for more information. Maybe using a transition_post_status hook would be better.

And now you need to add such a scheduled action to every post already published on your site. You need to use custom WP_Query loop over all the published posts and add a scheduled event for every one of them. This operation should be done only once, all new posts would have a reminder added via add_reminder function. The best approach I could think of is to do all of this as a plugin and put this loop inside a function that would be triggered on a plugin activation.

相关推荐

How to list posts by terms

我有一个带有自定义分类法和3个不同术语的自定义帖子类型。我正在尝试构建具有以下结构的页面:Term 1<贴子标记为术语1Term 2<用术语2标记的帖子Term 3<贴有术语3等的帖子。。。实现这一目标的最佳方式是什么?是否有\\u term()?