Plugin for changing a post’s category based on it’s post date?

Is there a plugin for WordPress that will change a post’s category based on how long that post has existed?

I have seen other WordPress plugins that use a library called “simple pie” to manage a wp-blog’s timed tasks, is there one that does something like this?

Related posts

Leave a Reply

1 comment

  1. Don’t know of a plugin but you can use wp_schedule_single_event function.

    First create a meta box that takes to values: time for removal and what category we want to set it to when removed from featured.

     /* hook meta box */
    add_action("admin_init", "admin_init");
    
    /* hook meta box function */
    function admin_init(){
        add_meta_box("Featured Removal", "Featured Removal", "Featured_Removal_options", "post", "normal", "high");
    }
    
    /* display meta box */
    function Featured_Removal_options() {
        global $post;
        $custom = get_post_custom($post->ID);
        echo '<input type="hidden" name="wp_meta_box_nonce" value="', wp_create_nonce('Featured Removal'), '" />';
        <?
        <table border=0>
        <tr>
            <th style="width:20%"><label for="Remove_after">Remove From Featured After:</label></th>
            <td><input type="text" name="Remove_after" id="Remove_after" value="<?php $custom['Remove_after'] ? $custom['Remove_after'] : ''; ?>"/><br/>
            Enter time in Seconds Ex: 1 Hour = 3600 Seconds , 1 Day = 86400 Seconds.
            </td>
        </tr>
        <tr>
            <th style="width:20%"><label for="Remove_after_to_cat">Remove From Featured To Category:</label></th>
            <td><input type="text" name="Remove_after_to_cat" id="Remove_after_to_cat" value="<?php $custom['Remove_after_to_cat'] ? $custom['Remove_after_to_cat'] : ''; ?>"/><br/>
            Enter the category id of the category you want to remove the post after the time has passed. if more then one separate by commas Ex: 12,13,24
            </td>
        </tr>
        </table>
    <?php }
    
    /* save meta box hook*/
    add_action('save_post', 'save_Featured_Removal_options');
    
    /* save meta box function*/
    function save_Featured_Removal_options($post_id) {
        if (!wp_verify_nonce($_POST['wp_meta_box_nonce'], "Featured Removal")) {
            return $post_id;
        }
        // check autosave
        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
            return $post_id;
        }
        If (isset($_POST['Remove_after']) && isset($_POST['Remove_after_to_cat'])){
            //cerate scheduled event
            $time = time() + $_POST['Remove_after'];
            wp_schedule_single_event($time, 'Clean_my_featured',$post_id);
            //save meta data
            update_post_meta($post_id, 'Remove_after', $_POST['Remove_after']);
            update_post_meta($post_id, 'Remove_after_to_cat', $_POST['Remove_after_to_cat']);
        }    
    }
    

    Now watch the save Meta Box function , if the user has enter both time for removal and category id for the new category then we set a scheduled event
    with wp_schedule_single_event and hook it to “Clean_my_featured“.

    So now we only need to add the action for that hook and the function for the removal itself:

     /* hook removal event function */
        add_action('Clean_my_featured','remove_post_from_featured');
    
    // the function that removes a post form a category and sets a new one
    function remove_post_from_featured($post_id) {
        $cats = get_post_meta($post_id, 'Remove_after_to_cat', true);
        wp_set_post_terms( $post_ID, $cats, 'category');
    }
    

    I have no Idea if this works but it should so just copy all of it to a plugin file or your themes functions.php file and it should work.

    if not let me know.