bbp_update_reply_position( int $reply_id, int $reply_position = false )
Update the position of the reply.
Description Description
The reply position is stored in the menu_order column of the posts table. This is done to prevent using a meta_query to retrieve posts in the proper freshness order. By updating the menu_order accordingly, we’re able to leverage core WordPress query ordering much more effectively.
Parameters Parameters
- $reply_id
-
(Required)
- $reply_position
-
(Optional)
Default value: false
Return Return
(mixed)
Source Source
File: includes/replies/functions.php
function bbp_update_reply_position( $reply_id = 0, $reply_position = false ) {
// Bail if reply_id is empty
$reply_id = bbp_get_reply_id( $reply_id );
if ( empty( $reply_id ) ) {
return false;
}
// Prepare the reply position
$reply_position = is_numeric( $reply_position )
? (int) $reply_position
: bbp_get_reply_position_raw( $reply_id, bbp_get_reply_topic_id( $reply_id ) );
// Get the current reply position
$current_position = get_post_field( 'menu_order', $reply_id );
// Bail if no change
if ( $reply_position === $current_position ) {
return false;
}
// Filters not removed
$removed = false;
// Toggle revisions off as we are not altering content
if ( has_filter( 'clean_post_cache', 'bbp_clean_post_cache' ) ) {
$removed = true;
remove_filter( 'clean_post_cache', 'bbp_clean_post_cache', 10, 2 );
}
// Update the replies' 'menu_order' with the reply position
$bbp_db = bbp_db();
$bbp_db->update( $bbp_db->posts, array( 'menu_order' => $reply_position ), array( 'ID' => $reply_id ) );
clean_post_cache( $reply_id );
// Toggle revisions back on
if ( true === $removed ) {
$removed = false;
add_filter( 'clean_post_cache', 'bbp_clean_post_cache', 10, 2 );
}
return (int) $reply_position;
}
Changelog Changelog
| Version | Description |
|---|---|
| 2.1.0 | Introduced. |