BP_Messages_Thread::get_messages( int $thread_id, $before = null, $perpage = 10 )
Get all messages associated with a thread.
Description
Parameters
- $thread_id
-
(Required) The message thread ID.
Return
(object) List of messages associated with a thread.
Source
File: bp-messages/classes/class-bp-messages-thread.php
public static function get_messages( $thread_id = 0, $before = null, $perpage = 10 ) { $thread_id = (int) $thread_id; $cache_key = "{$thread_id}{$before}{$perpage}"; $messages = wp_cache_get( $cache_key, 'bp_messages_threads' ); if ( false === $messages || static::$noCache ) { // if current user isn't the recpient, then return empty array if ( ! static::is_thread_recipient( $thread_id ) ) { wp_cache_set( $cache_key, [], 'bp_messages_threads' ); return []; } global $wpdb; $bp = buddypress(); $last_deleted_id = static::$last_deleted_message ? static::$last_deleted_message->id : 0; $last_deleted_timestamp = static::$last_deleted_message? static::$last_deleted_message->date_sent : '0000-00-00 00:00:00'; if ( ! $before ) { $before = bp_core_current_time(); // $before = gmdate( 'Y-m-d H:i:s', ( time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS + 1 ) ) ); } // Always sort by DESC by default. $messages = $wpdb->get_results( $sql = $wpdb->prepare( "SELECT * FROM {$bp->messages->table_name_messages} WHERE thread_id = %d AND id > %d AND date_sent > %s AND date_sent <= %s ORDER BY date_sent DESC, id DESC LIMIT %d ", $thread_id, $last_deleted_id, $last_deleted_timestamp, $before, $perpage ) ); wp_cache_set( $cache_key, (array) $messages, 'bp_messages_threads' ); } // Integer casting. foreach ( $messages as $key => $data ) { $messages[ $key ]->id = (int) $messages[ $key ]->id; $messages[ $key ]->thread_id = (int) $messages[ $key ]->thread_id; $messages[ $key ]->sender_id = (int) $messages[ $key ]->sender_id; } return $messages; }
Changelog
Version | Description |
---|---|
BuddyPress 2.3.0 | Introduced. |
Questions?
We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.