Skip to:
Content
Pages
Categories
Search
Top
Bottom

Getting Last Topic or Post Date

  • @nik_s

    Participant

    WordPress Version: 4.5.3
    bbPress Version: 2.5.9
    Website Link: http://www.sva.bc.ca/newforums/

    Hello,

    After setting up bbPress I “imported” forum posts manually from an older system, setting the “Published On” date to match the date of each original topic/reply. I then found that bbPress’s “freshness” ignored this date and used the date/time I had manually added these posts.

    So I installed the bbPress last post plugin ( https://en-gb.wordpress.org/plugins/bbp-last-post/ ) and modified it a little. The following code is what is currently being used, and is almost what I want:

    <?php
    
    //this function changes the bbp freshness data (time since) into a last post date for forums
    function change_freshness_forum ($forum_id = 0 ) {
    
    // Verify forum and get last active meta
    		$forum_id         = bbp_get_forum_id( $forum_id );
    
    			$reply_id = bbp_get_forum_last_reply_id( $forum_id );
    			if ( !empty( $reply_id ) ) {
    				$last_active_date = get_the_date( '', $reply_id );
    			} else {
    				$topic_id = bbp_get_forum_last_topic_id( $forum_id );
    				if ( !empty( $topic_id ) ) {
    					$last_active_date = get_the_date('', $topic_id );
    				}
    			}
    
    			if ( !empty( $reply_id ) ) {
    				$last_active_time = get_the_time( '', $reply_id );
    			} else {
    				if ( !empty( $topic_id ) ) {
    					$last_active_time = get_the_time('', $topic_id );
    				}
    			}
    			
    		$last_active_date = bbp_convert_date( $last_active_date ) ;
    		$last_active_time = bbp_convert_date( $last_active_time ) ;
    		$date_format = get_option( 'date_format' );
    		$time_format = get_option( 'time_format' );
    		$date= date_i18n( "{$date_format}", $last_active_date );
    		$time=date_i18n( "{$time_format}", $last_active_time );
    		$active_time = sprintf( _x( '%1$s at %2$s', 'date at time', 'bbp-last-post' ), $date, $time );  
    		return $active_time ;
    		}
    add_filter( 'bbp_get_forum_last_active', 'change_freshness_forum', 10, 2 );
    
    //this function changes the bbp freshness data (time since) into a last post date for topics
    function change_freshness_topic ($last_active, $topic_id) {
    
    $topic_id = bbp_get_topic_id( $topic_id );
    
    		// Try to get the most accurate freshness date possible
    		if ( empty( $last_active_date ) ) {
    		$reply_id = bbp_get_topic_last_reply_id( $topic_id );
    		if ( !empty( $reply_id ) ) {
    			$last_active_date = get_the_date( '', $reply_id );
    		} else {
    				$last_active_date = get_the_date( '', $topic_id );
    			}
    		}
    
    		// Try to get the most accurate freshness time possible
    		if ( empty( $last_active_time ) ) {
    		$reply_id = bbp_get_topic_last_reply_id( $topic_id );
    		if ( !empty( $reply_id ) ) {
    			$last_active_time = get_the_time( '', $reply_id );
    		} else {
    				$last_active_time = get_the_time( '', $topic_id );
    			}
    		}
    		
    		
    		$last_active_date = bbp_convert_date( $last_active_date ) ;
    		$last_active_time = bbp_convert_date( $last_active_time ) ;
    		$date_format = get_option( 'date_format' );
    		$time_format = get_option( 'time_format' );
    		$date= date_i18n( "{$date_format}", $last_active_date );
    		$time=date_i18n( "{$time_format}", $last_active_time );
    		$active_time = sprintf( _x( '%1$s at %2$s', 'date at time', 'bbp-last-post' ), $date, $time );  
    		return $active_time ;
    		}
    add_filter( 'bbp_get_topic_last_active', 'change_freshness_topic', 10, 2 );
    
    //This function changes the heading "Freshness" to the name created in Settings>bbp last post
    function change_translate_text( $translated_text ) {
    	$text = 'Freshness' ;
    	if ( $translated_text == $text ) {
    	global $rlp_options;
    	$translated_text = $rlp_options['heading_label'];
    	}
    	return $translated_text;
    }
    add_filter( 'gettext', 'change_translate_text', 20 );

    The only problem now is that from the forum index view, the “Last Post” column will show the date of the most recent reply, even if there is a newer topic.

    Is there a way to compare the dates and make sure the Last Post column displays the most recent topic or reply, whichever is newer?

    Thanks!

Viewing 8 replies - 1 through 8 (of 8 total)
  • @nik_s

    Participant

    Managed to solve the problem using this code:

    <?php
    
    //this function changes the bbp freshness data (time since) into a last post date for forums
    function change_freshness_forum ($forum_id = 0 ) {
    
    // Verify forum and get last active meta
    		$forum_id = bbp_get_forum_id( $forum_id );
    
    			// Get the date for the most recent reply and topic in each forum
    			$reply_id = bbp_get_forum_last_reply_id( $forum_id );
    			if ( !empty( $reply_id ) ) {
    				$last_active_reply_date = get_the_date( '', $reply_id );
    			}
    			$topic_id = bbp_get_forum_last_topic_id( $forum_id );
    			if ( !empty( $topic_id ) ) {
    				$last_active_topic_date = get_the_date('', $topic_id );
    			}
    
    			// Compare the reply and topic dates, and assign the most recent one to $last_active_date
    				if ( !empty($last_active_reply_date ) && !empty($last_active_topic_date) ) {
    					$last_active_date = ((strtotime($last_active_topic_date) >= strtotime($last_active_reply_date)) ? $last_active_topic_date : $last_active_reply_date );
    				} elseif (empty($last_active_reply_date)) {
    					$last_active_date = $last_active_topic_date ;
    				} else {
    					$last_active_date = $last_active_reply_date ;
    				}
    
    			// Get the time for the most recent reply and topic in each forum
    			if ( !empty( $reply_id ) ) {
    				$last_active_reply_time = get_the_time( '', $reply_id );
    			}
    			if ( !empty( $topic_id ) ) {
    				$last_active_topic_time = get_the_time('', $topic_id );
    			}
    
    			// Compare the reply and topic times, and assign the most recent one to $last_active_time
    				if ( !empty($last_active_reply_time ) && !empty($last_active_topic_time) ) {
    					$last_active_time = ((strtotime($last_active_topic_time) >= strtotime($last_active_reply_time)) ? $last_active_topic_time : $last_active_reply_time );
    				} elseif (empty($last_active_reply_time)) {
    					$last_active_time = $last_active_topic_time ;
    				} else {
    					$last_active_time = $last_active_reply_time ;
    				}
    			
    		$last_active_date = bbp_convert_date( $last_active_date ) ;
    		$last_active_time = bbp_convert_date( $last_active_time ) ;
    		$date_format = get_option( 'date_format' );
    		$time_format = get_option( 'time_format' );
    		$date= date_i18n( "{$date_format}", $last_active_date );
    		$time=date_i18n( "{$time_format}", $last_active_time );
    		$active_time = sprintf( _x( '%1$s at %2$s', 'date at time', 'bbp-last-post' ), $date, $time );  
    		return $active_time ;
    		}
    add_filter( 'bbp_get_forum_last_active', 'change_freshness_forum', 10, 2 );
    
    //this function changes the bbp freshness data (time since) into a last post date for topics
    function change_freshness_topic ($last_active, $topic_id) {
    
    $topic_id = bbp_get_topic_id( $topic_id );
    
    		// Try to get the most accurate freshness date possible
    		if ( empty( $last_active_date ) ) {
    		$reply_id = bbp_get_topic_last_reply_id( $topic_id );
    		if ( !empty( $reply_id ) ) {
    			$last_active_date = get_the_date( '', $reply_id );
    		} else {
    				$last_active_date = get_the_date( '', $topic_id );
    			}
    		}
    
    		// Try to get the most accurate freshness time possible
    		if ( empty( $last_active_time ) ) {
    		$reply_id = bbp_get_topic_last_reply_id( $topic_id );
    		if ( !empty( $reply_id ) ) {
    			$last_active_time = get_the_time( '', $reply_id );
    		} else {
    				$last_active_time = get_the_time( '', $topic_id );
    			}
    		}
    		
    		
    		$last_active_date = bbp_convert_date( $last_active_date ) ;
    		$last_active_time = bbp_convert_date( $last_active_time ) ;
    		$date_format = get_option( 'date_format' );
    		$time_format = get_option( 'time_format' );
    		$date= date_i18n( "{$date_format}", $last_active_date );
    		$time=date_i18n( "{$time_format}", $last_active_time );
    		$active_time = sprintf( _x( '%1$s at %2$s', 'date at time', 'bbp-last-post' ), $date, $time );
    		return $active_time ;
    		}
    add_filter( 'bbp_get_topic_last_active', 'change_freshness_topic', 10, 2 );
    
    //This function changes the heading "Freshness" to the name created in Settings>bbp last post
    function change_translate_text( $translated_text ) {
    	$text = 'Freshness' ;
    	if ( $translated_text == $text ) {
    	global $rlp_options;
    	$translated_text = $rlp_options['heading_label'];
    	}
    	return $translated_text;
    }
    add_filter( 'gettext', 'change_translate_text', 20 );

    But now another issue has become apparent – the permalinks and anchor tag titles still refer to either bbp_get_forum_last_topic_permalink or bbp_get_forum_last_reply_url (I think) and not to the most recent post or topic as I have defined it above. Any ideas on how I can change the last_topic or last_reply permalinks to match the ones corresponding to the displayed dates? This is just a touch beyond my current PHP skills and so would greatly appreciate any suggestions.

    @netweb

    Keymaster

    Did you run the “repair tools” to repair the forums metadata after your “manual import”?

    @nik_s

    Participant

    Yes, I tried all the repair tools sequentially.

    To clarify, by manual import I mean I added each topic and reply by hand from the WordPress dashboard and a plugin to change the post authors.

    Main issue now is I that I don’t know how to change the permalinks to match the ones corresponding to the “Published Date” I retroactively selected for each post, rather than the date I created those posts.

    @netweb

    Keymaster

    What do you have your permalinks set to? /wp-admin/options-permalink.php?

    @nik_s

    Participant

    Hi,

    The permalinks are set to “Post name”.

    @mbfit

    Participant

    Hi if you’re still around would like to know how you changed Freshness to Last Post?

    Thanks.

    @netweb

    Keymaster

    Hi if you’re still around would like to know how you changed Freshness to Last Post?

    bbPress 2.6 will no longer use “Freshness”, the default is now “Last Post”

    @mbfit

    Participant

    Ah ok thanks! I did find the code for that on here

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