Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I need to clean up my database and I think that a way to do and keep it that way is by deleting all posts that have 3 days of life. One good thing is that those posts I want to delete are in a specific category.

So, how can I clean up my database correctly by deleting or moving thoem and also their posts_meta or terms or anything ? I want to just have/show the posts that are 3 days or earlier.

share|improve this question
1  
what would you do about the returning 404 errors for when these pages are indexed, if your deleting posts after 3 days it sounds like you have a content rich site and with continually added content i would dare say these 3 day old pages would be getting indexed pretty quickly.. –  MartinJJ May 16 '11 at 14:27
    
You mean indexed from google? Basically I use my page as a "share your post" with their permalink and stuf through tdo mini forms. –  Xalloumokkelos May 16 '11 at 14:35
    
Rather than deleting the actual content, have you thought about altering your theme so it only shows the content from the last 3 days? –  Manzabar May 16 '11 at 14:44
    
Well yes, but will this speed up my system? I have 70.000 posts, I think that even a single query lasts longer. –  Xalloumokkelos May 16 '11 at 14:50
    
You have to send a 410 status header or your page trust will fall like a stone. It is easier just to close the site. –  toscho May 16 '11 at 17:32

1 Answer 1

up vote 1 down vote accepted

here is a quick plugin i cooked for you :)

 <?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 ); 
    }
}
share|improve this answer
    
Hi thanks for this. So what it does, it auto deletes expired posts (3 days) and anything related to it right? How can I delete or transfer in another database all the posts on a specific category and anything related to it? –  Xalloumokkelos May 16 '11 at 18:31
    
Bainternet do you accept work as a freelancer? I feel I will need you for some simple scripts quite soon. –  Xalloumokkelos May 16 '11 at 18:44
    
@punkis It adds a new metabox which lets you select if you want the post or the page auto deleted it self after any amount of time you specify. and yes i do accept work as a freelancer. –  Bainternet May 17 '11 at 4:57
    
Ok then! I will need you in a month for some scripts. How can I contact you ? –  Xalloumokkelos May 18 '11 at 22:59
    
click my image under the answer or anywhere on this site you see my name, from there you can see the link to my site , feel free to do so. –  Bainternet May 19 '11 at 6:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.