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');
}
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" ) );
}
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