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.

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?

share|improve this question
1  
Can you give more specific details? What's your use-case? What categories do you want to set before and what do you want them changed to? Are categories correct, or would a custom taxonomy make more sense? –  MikeSchinkel Jan 24 '11 at 1:09
    
@MikeSchinkel Well the deal is that I've got posts that I want to "expire" and move to a different part of my template, making them no longer the featured story. The template that I'm using displays them in different places depending on what category they're in. I'd like for this to happen automatically after a set number of days (or when new posts come in if possible). –  leeand00 Jan 24 '11 at 1:29
    
I assume you have a 'featured' category and you want to simply remove it, right? Or do you have something like 'featured-sports' and you want to move to 'sports'? Details are really helpful, and updating your question with details rather than just responding in comments is even more helpful. It would not be hard to write a custom snippet of code to do that hardcodes your logic. Creating an admin interface to allow someone to configure it would be an order of magnitude more work. Hence one reason of many that details of your use-case are helpful. –  MikeSchinkel Jan 24 '11 at 1:43
    
@MikeSchinkel Yes, I would like to remove a post from a category called 'featured-sports' to a category called 'sports', that's the idea. It's almost like a post-state if you will. –  leeand00 Jan 24 '11 at 13:51
    
@leeadn00 - Well I see בניית אתרים has given you the code so I'll leave you in his capable hands. –  MikeSchinkel Jan 24 '11 at 22:25

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
    
Wow thank you. Is there additionally a hook for when a new post is created? I know there's a list of hooks here: adambrown.info/p/wp_hooks/version/3.0 –  leeand00 Jan 24 '11 at 14:00
    
Hmm is this it? adambrown.info/p/wp_hooks/hook/transition_post_status –  leeand00 Jan 24 '11 at 14:24
    
i don't think so, why? –  Bainternet Jan 24 '11 at 15:16
    
Well I just figured that hooks were like they are in Subversion where they are run when some event happens (kind of like event listeners) –  leeand00 Jan 25 '11 at 3:00

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.