• Hi guys,

    Is it possible for wordpress to automatically change the category assigned to a post after a certain amount of time? For example, i have posts that are often assigned to multiple categories such as ‘golf’, ‘nike’, and ‘new this week’. After 7 days i would like any posts assigned to the category of ‘new this week’ to be stripped of this. Any help would be much appreciated.

    I have looked online and found an article that appears to offer a solution but it doesnt work with me. Here is the link…

    https://wordpress.stackexchange.com/questions/7571/plugin-for-changing-a-posts-category-based-on-its-post-date

    This uses the wp_schedule_single_event as a basis.

    Thanks in advance.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hello,

    That code should work fine..
    Well, if you need always remove the category “new this week” after 7 days. You can try this code (in your functions.php).

    add_action('save_post', 'save_Featured_Removal_options');
    
    function save_Featured_Removal_options($post_id) {
        // check autosave
        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
            return $post_id;
        }
    		//create scheduled event (7 days)
    		$time = time() + 604800;
    		wp_schedule_single_event($time, 'clean_my_featured',$post_id);
        }
    }
    
    /* 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) {
        $cat_id = get_cat_ID('new this week');
        wp_set_post_terms( $post_id, $cat_id, 'category');
    }

    Thread Starter paa1605

    (@paa1605)

    Thanks for the reply WillxD, much appreciated. I copied and pasted the code you provided into functions.php and changed the time from 604800 to 60 seconds so i could test it, yet i still don’t see anything happening. Am i doing something wrong or is there anything else i need to change? Sorry for the obvious questions but im quite a novice with php.

    Thanks,
    patrick

    I’m sorry, I hadn’t tried the previous code. Well, here is the new code. I hope it helps.

    add_action('publish_post', 'remove_cat_gen');
    
    function remove_cat_gen() {
    		global $post;
    		wp_schedule_single_event(time()+604800, 'clean_my_post', array($post->ID) );
    }
    
    add_action('clean_my_post','remove_cat_from_post');
    
    // the function that removes the category from the post
    function remove_cat_from_post($post_id) {
        global $wpdb;
    		$cat_id = get_cat_ID('new this week');
    		$wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->term_relationships WHERE object_id = $post_id AND term_taxonomy_id = $cat_id" ) );
    }
    Thread Starter paa1605

    (@paa1605)

    Fantastic WillxD!!! That works perfectly. A massive thanks.

    Can i just ask something else. If i have a post assigned to several categories such as ‘golf’, ‘nike’, ‘new this week’ and ‘new this month’ is it possible to add something to the code you provided so that it automatically removes itself from the category of ‘new this week’ after 7 days, and also removes itself from the category of ‘new this month’ after 30 days??

    If you could get the code to do this it would be a perfect solution to my problem.

    Thanks again for all your kind work so far. Very much appreciated.

    Regards,
    Patrick

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to automatically change the category assigned to a post?’ is closed to new replies.