Function experts? Help tweak this code for me please
-
Hi guys,
I asked for some help a couple of weeks ago at making WordPress automatically remove a post from a certain category after a set length of time. For example, remove a post from the category of ‘new this week’ after 7 days. I received a solution from one member that worked perfectly. The code is as follows and must be copied and pasted into functions.php
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" ) ); }
I asked a follow-up question that unfortunately was met with no response so i turn to you guys in the hope that you can help me.
If i have a post assigned to several categories such as ‘sport’, ‘golf’, ‘new this week’ and ‘new this month’ is it possible to add something to the code above 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??
Any help is very much appreciated. Thanks.
- The topic ‘Function experts? Help tweak this code for me please’ is closed to new replies.