你对我的想法是“3天后删除或移动特定类别中的所有帖子”

时间:2011-05-16 作者:EnexoOnoma

我需要清理我的数据库,我认为一种方法是删除所有有3天生命的帖子。一件好事是,我想删除的帖子属于特定类别。

那么,我如何通过删除或移动theem以及他们的post\\u meta或terms或任何东西来正确清理我的数据库呢?我只想拥有/显示3天或更早的帖子。

1 个回复
最合适的回答,由SO网友:Bainternet 整理而成

下面是我为您制作的一个快速插件:)

 <?php
/*
Plugin Name: Post Auto Removal
Plugin URI: http://en.bainternet.info
Description: Post Auto Removal lets you schedule when the post / page / custom type will be deleted automatically.
Version: 0.1
Author: bainternet
Author URI: http://en.bainternet.info
*/


/* hook meta box */
add_action("admin_init", "admin_init");

/* hook meta box function */
function admin_init(){
    add_meta_box("Post Auto Removal", "Post Auto Removal", "Post_Auto_Removal_options", "post", "normal", "high");
    add_meta_box("Post Auto Removal", "Post Auto Removal", "Post_Auto_Removal_options", "page", "normal", "high");
}

/* display meta box */
function Post_Auto_Removal_options() {
    global $post;
    $custom[\'active_removal\'] = get_post_meta($post->ID,\'active_removal\',true);
    $custom[\'Remove_after\'] = get_post_meta($post->ID,\'Remove_after\',true);
    echo \'<input type="hidden" name="wp_meta_box_nonce" value="\'. wp_create_nonce(\'Auto_Removal\'). \'" />\';
?>
    <table border=0>
      <tr>
        <th style="width:20%"><label for="active_removal">Activate auto removal:</label></th>
        <td><input type="checkbox" name="active_removal" id="active_removal" value="yes" <?php echo $custom[\'active_removal\'] ? \'checked\' : \'\'; ?>/><br/>
        when check post will be deleted in the given time.
        </td>
    </tr>
    <tr>
        <th style="width:20%"><label for="Remove_after">Delete post after:</label></th>
        <td><input type="text" name="Remove_after" id="Remove_after" value="<?php echo $custom[\'Remove_after\'] ? $custom[\'Remove_after\'] : \'\'; ?>"/><br/>
        Enter time in Seconds Ex: 1 Hour = 3600 Seconds , 1 Day = 86400 Seconds.
        </td>
    </tr>
    <?php  
    $next = wp_get_schedule( \'Clean_my_posts\', array($post_id));
    print_r($next);
    if ($next){ ?>
    <tr>
    <th>next schedule is set to : <?php echo $next; ?></th>
    </tr>
    <?php } ?>

    </table>
<?php }

/* save meta box hook*/
add_action(\'save_post\', \'save_Post_Auto_Removal_options\');

/* save meta box function*/
function save_Post_Auto_Removal_options($post_id) {
    if (!wp_verify_nonce($_POST[\'wp_meta_box_nonce\'], "Auto_Removal")) {
        return $post_id;
    }
    // check autosave
    if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) {
        return $post_id;
    }
    If (isset($_POST[\'Remove_after\']) && isset($_POST[\'active_removal\'])){
        //cerate scheduled event
        $time = time() + $_POST[\'Remove_after\'];
        wp_schedule_single_event($time, \'Clean_my_posts\',array($post_id));
        //save meta data
        update_post_meta($post_id, \'Remove_after\', $_POST[\'Remove_after\']);
        update_post_meta($post_id, \'active_removal\', $_POST[\'active_removal\']);

    }    
}


 /* hook removal event function */
  add_action(\'Clean_my_posts\',\'auto_remove_post\',1,1);

// the function that deletes a post everything that is tied to it, This includes comments, post meta fields, and terms associated with the post.
function auto_remove_post($post_id) {
    $delete = get_post_meta($post_id, \'active_removal\', true);
    if ($delete){
        wp_delete_post( $post_id, true ); 
    }
}

结束

相关推荐

Preview posts returns 404

这是一个大型Wordpress多站点安装,使用单个博客的子文件夹,而不是域。我们正在运行3.1版,预览帖子不起作用。当我按preview时,我得到了404。php页面。日志文件中没有引起我注意的内容,所有插件都已禁用。有时,当我按preview时,会收到一条关于权限不足的错误消息。我没有办法尝试了。