Put this in your child theme’s function file – or use
Code Snippets
change the ‘-10 days’ to say ‘-90 days’ if you want 3 months
register_activation_hook(__FILE__, 'bbpress_topic_scheduler');
add_action('bbpress_daily_event', 'bbpress_close_old_topics');
function bbpress_topic_scheduler() {
wp_schedule_event(time(), 'daily', 'bbpress_daily_event');
}
function bbpress_close_old_topics() {
// Auto close old topics
$topics_query = array(
'author' => 0,
'show_stickies' => false,
'parent_forum' => 'any',
'post_status' => 'publish',
'posts_per_page' => -1
);
if ( bbp_has_topics( $topics_query ) )
while( bbp_topics() ) {
bbp_the_topic();
$topic_id = bbp_get_topic_id();
$last_active = strtotime( get_post_meta( $topic_id, '_bbp_last_active_time', true ) );
if ($last_active < strtotime( '-10 days') )
bbp_close_topic( $topic_id );
}
}
The above will ‘close’ the topic, if you want it archived, then in effect you will need to trash it, this just puts it in the trash, so it can be recovered
to do this replace
bbp_close_topic( $topic_id );
with
bbp_trash_topic( $topic_id );
Thanks, Robin. Am I able to do this for topics in specific forums? Rather than all…Thanks… Jo
yes, just change the line
'parent_forum' => 'any',
to
'parent_forum' => '123456',
you can find a forums number by looking at the url whilst you are editing it
dashboard>forums>all forums>edit (the forum you want) and in the url bar you will see
http://www.mysite.com/wp-admin/post.php?post=28109&action=edit
if you want several change
'parent_forum' => 'any',
to
'post_parent__in' => array( 2, 5, 12, 14, 20 ),
Thanks, Robin. One last question π If I want it to delete topics I presume I change the code
bbp_close_topic( $topic_id );
to
bbp_delete_topic( $topic_id );
A lot of the posts I want to get rid of have images so trying to clean up and free space… thanks again!
Robin, sorry another quick question do I have to replace all references to ‘close’ with ‘delete instead in the other sections of the code? e.g in this one
add_action(‘bbpress_daily_event’, ‘bbpress_close_old_topics’)
Thanks… sorry I’m not more up on this kind of thing…
no need to change them – they are only names, although you could change
add_action('bbpress_daily_event', 'bbpress_close_old_topics');
to
add_action('bbpress_daily_event', 'bbpress_delete_old_topics');
and
function bbpress_close_old_topics() {
// Auto close old topics
to
function bbpress_delete_old_topics() {
// Auto delete old topics
which would just make it clearer what it was doing – but not necessary !!
Thanks so much, help much appreciated.
Jo