用于自动删除超过x天的特定帖子类型的帖子的Cron作业

时间:2016-01-05 作者:jackennils

我想删除超过60天的特定帖子类型(在本例中为“vfb\\U条目”)的所有帖子。cron作业应每天运行一次。

我有以下代码,但如果我触发cron作业,它将不起作用。但是,仅在phpMyAdmin中运行查询会返回正确的结果。所以功能本身肯定有问题。

有人能帮忙吗?

// Cron Job to Delete VFB Entries older than 60 days
if(!wp_next_scheduled( \'remove_old_vfb_entries\')){
    wp_schedule_event(time(), \'daily\', \'remove_old_vfb_entries\');
}
add_action(\'remove_old_vfb_entries\', \'myd_remove_old_vfb_entries\');

// Build the function
function myd_remove_old_vfb_entries(){
global $wpdb;
// Set max post date and post_type name
$date = date("Y-m-d H:i:s", strtotime(\'-60 days\'));
$post_type = \'vfb_entry\';

// Build the query 
// Only select VFB Entries from LFG or LFM Form
$query = "
    SELECT $wpdb->posts.ID FROM $wpdb->posts 
    WHERE post_type = \'$post_type\' 
    AND post_status = \'publish\' 
    AND post_date < \'$date\'
    AND ($wpdb->posts.ID IN (SELECT entry_id FROM wp_postmeta_lfg4 WHERE post_id IS NOT NULL) OR $wpdb->posts.ID IN (SELECT entry_id FROM wp_postmeta_lfm3 WHERE post_id IS NOT NULL))
    ORDER BY post_modified DESC
";
$results = $wpdb->get_results($query);
            foreach($results as $post){
                // Let the WordPress API clean up the entire post trails
                wp_delete_post( $post->ID, true);
          }
}
Edit: 解决方案无需查询我的视图,只需使用wp\\u posts和wp\\u posmeta以及下面的内部连接。

3 个回复
SO网友:MagniGeeks Technologies

// add the schedule event if it has been removed 
if( ! wp_next_scheduled( \'mg_remove_old_entries\' ) ) {
  wp_schedule_event( time(), \'daily\', \'mg_remove_old_entries\' ); //run the event daily
}

// action hooked to fired with wordpress cron job
add_action( \'mg_remove_old_entries\', \'mg_remove_old_entries\' );
function mg_remove_old_entries() {
  $posts = get_posts( [
    \'numberposts\' => -1,
    \'post_type\' => \'vfb_entry\',
    \'date_query\' => [
      // get all the posts from the database which are older than 60 days
      \'before\' => date( "Y-m-d H:i:s", strtotime( \'-60 days\' ) ),
    ],
  ]);

  if( !empty($posts) ) {
    foreach( $posts as $post ) {
      wp_delete_post( $post->ID ); //remove the post from the database
    }
  }
}
Note: 无需运行任何自定义sql查询。这会使查询速度变慢,而且对wordpress也不好。Wordpress已经内置了各种功能。

SO网友:jackennils
// Cron Job to Delete VFB Entries older than 60 days
if(!wp_next_scheduled( \'remove_old_vfb_entries\')){
    wp_schedule_event(time(), \'daily\', \'remove_old_vfb_entries\');
}
add_action(\'remove_old_vfb_entries\', \'myd_remove_old_vfb_entries\');

// Build the function
function myd_remove_old_vfb_entries(){
global $wpdb;
// Set max post date and post_type name
$date = date("Y-m-d H:i:s", strtotime(\'-60 days\'));
$post_type = \'vfb_entry\';

// Build the query 
// Only Delete Entries from Form 5 and 8
$query = "
    SELECT $wpdb->posts.ID FROM $wpdb->posts 
    INNER JOIN $wpdb->postmeta 
      ON $wpdb->posts.ID = $wpdb->postmeta.post_id 
    WHERE post_type = \'$post_type\' 
    AND post_status = \'publish\' 
    AND $wpdb->postmeta.meta_key = \'_vfb_form_id\' 
    AND ($wpdb->postmeta.meta_value = 5 OR $wpdb->postmeta.meta_value = 8) 
    AND post_date < \'$date\' 
";
$results = $wpdb->get_results($query);
            foreach($results as $post){
                // Let the WordPress API clean up the entire post trails
                wp_delete_post( $post->ID, true);
          }
}
SO网友:Nathan Johnson

我想删除超过60天的特定帖子类型(在本例中为“vfb\\U条目”)的所有帖子。cron作业应每天运行一次。

第一步是设置cron作业。问题中的代码对于这一部分是正确的,因此它基本上复制到下面。

第二部分需要在数据库中查询条目超过60天的特定帖子类型。我们可以用get_posts() 并指定post_type 参数和date_query 论点

//* If the scheduled event got removed from the cron schedule, re-add it
if( ! wp_next_scheduled( \'wpse_213720_remove_old_entries\' ) ) {
  wp_schedule_event( time(), \'daily\', \'wpse_213720_remove_old_entries\' );
}

//* Add action to hook fired by cron event
add_action( \'wpse_213720_remove_old_entries\', \'wpse_213720_remove_old_entries\' );
function wpse_213720_remove_old_entries() {
  //* Get all the custom post type entries older than 60 days...
  $posts = get_posts( [
    \'numberposts\' => -1,
    \'post_type\' => \'wpse_262471_post_type\',
    \'date_query\' => [
      \'before\' => date( "Y-m-d H:i:s", strtotime( \'-60 days\' ) ),
    ],
  ]);
  //* ...and delete them
  array_filter( function( $post ) {
    wp_delete_post( $post->ID );
  }, $posts );
}
以上答案直接询问$wpdb. 使用get_posts() 由于各种原因,抽象性更好,包括易于阅读和将来的校对。

相关推荐

将wp_Query替换为wp_User_Query

我在插件中制作了一些订阅者档案和单曲(个人资料页),其中我还包括了一个“用户单曲”模板,通过template_include. 不过,我正在尝试从插件中删除一些模板,以使其使用主题模板。我用了locate_template( \'single.php\' ) 从活动主题中选择单个模板。我没有使用全局wp_query 在本例中显示我的内容,但页面显示了基于查询默认值的循环(十篇帖子)。我想知道的是,我是否可以完全放弃默认查询,用wp_user_query 我可以将查询到的用户ID输入其中。然后我想筛选the