WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

For each new post, I add News category.

And I want to delete this category from each post after 7 days. I already know to "delete" the new category but how to run it every week ?

share|improve this question
up vote 3 down vote accepted

Did you try this plugin? http://wordpress.org/extend/plugins/scheduled-post-delete/


Sorry for my mistake, I understood you wrong. Try by pasting this in your functions.php:

function auto_cat_remove() {

    global $post;
    wp_schedule_single_event( time() + 604800, 'remove_news_cat_event', array( $post->ID ) );


function remove_news_cat_func( $post_id ) {
    global $wpdb;
    $category_id = get_cat_ID( 'news' );
    $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id = %d", $post_id, $category_id ) );

add_action( 'publish_post', 'auto_cat_remove' );
add_action( 'remove_news_cat_event', 'remove_news_cat_func' );

What this will do is basically schedule event seven days after the post creation. On 7th day, remove_news_cat_func function will run that will remove "news" category from the list of categories assigned to that post.

If you want to test it, you can change 604800 in wp_schedule_single_event to something like 30 and if you do that, 30 seconds after post creation category should be gone.

Note that this function relies on WP-Cron so to fire it, someone actually has to visit the site (this might cause delay with short span of 30 seconds but with days you are fine).

share|improve this answer
I don't want to delete post, and I want to delete the category "News" of the post. – Steffi Jan 11 '13 at 23:10
Answer updated. – OriginalEXE Jan 11 '13 at 23:24
Yes, that's what it will do. I fail to see practical value of scheduling event weekly because then posts that were for example published on the 6th day will only stay in the news category for one day. – OriginalEXE Jan 12 '13 at 0:04
Yes, correct, good remainder, I updated the answer for future wonderers :) – OriginalEXE Jan 12 '13 at 0:30
Lol :) Try specifying the id directly then, $category_id = 55; or whatever id it is. You can see id of the category when you go to category edit screen (just look for the tag id in the url). – OriginalEXE Jan 12 '13 at 0:39

I found the solution. Thanks @OriginalEXE for the cron.

add_action( 'publish_post', 'auto_cat_remove' );
add_action( 'remove_news_cat_event', 'remove_cat_from_post' );

function auto_cat_remove() {
    global $post;
    wp_schedule_single_event( time() + 604800, 'remove_news_cat_event', array( $post->ID ) );

function remove_cat_from_post( $post_id ) {

    $term = 419; // Your cat ID
    $taxonomy = "category";

    if ( ! is_numeric( $term ) ) {
        $term = get_term( $term, $taxonomy );
        if ( ! $term || is_wp_error( $term ) )
           return false;
        $term_id = $term->term_id;
    } else {
        $term_id = $term;

    // Get the existing terms and only keep the ones we don't want removed
    $new_terms = array();
    $current_terms = wp_get_object_terms( $post_id, $taxonomy, array( 'fields' => 'ids' ) );

    foreach ( $current_terms as $current_term ) {
       if ( $current_term != $term_id )
            $new_terms[] = intval( $current_term );

    return wp_set_object_terms( $post_id, $new_terms, $taxonomy );
share|improve this answer
Really great, I am glad you found solution. I still fail to understand where my code fails but what's important is that you got your answer :) – OriginalEXE Jan 12 '13 at 16:23
I think $wpdb must be $wpdb->prefix because in my database it's not "wp_" but "wp_new" – Steffi Jan 12 '13 at 21:49

Your Answer


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.