Skip to:
Content
Pages
Categories
Search
Top
Bottom

Automatic Archive Settings for Topics


  • writersabroad
    Participant

    @writersabroad

    Writers Abroad
    Wordpress version 5.3.2–en_GB

    Is there a programme/plugin for Archive settings where I can archive topics after a given amount of time, e.g. 3 months, to tidy up forums?
    Thanks in advance
    Jo

Viewing 10 replies - 1 through 10 (of 10 total)

  • Robin W
    Moderator

    @robin-w

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

    Robin W
    Moderator

    @robin-w

    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 );


    writersabroad
    Participant

    @writersabroad

    Thanks, Robin. Am I able to do this for topics in specific forums? Rather than all…Thanks… Jo


    Robin W
    Moderator

    @robin-w

    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


    Robin W
    Moderator

    @robin-w

    if you want several change

    'parent_forum' => 'any',

    to

    'post_parent__in' => array( 2, 5, 12, 14, 20 ),


    writersabroad
    Participant

    @writersabroad

    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 W
    Moderator

    @robin-w

    yes, should be


    writersabroad
    Participant

    @writersabroad

    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…


    Robin W
    Moderator

    @robin-w

    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 !!


    writersabroad
    Participant

    @writersabroad

    Thanks so much, help much appreciated.
    Jo

Viewing 10 replies - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.
Skip to toolbar