Store story comment count in post row

Primarily for query purposes.
This commit is contained in:
Tetrakern 2024-11-10 14:46:36 +01:00
parent 404479d131
commit 7aec3c55af
3 changed files with 58 additions and 0 deletions

View File

@ -937,3 +937,51 @@ if ( ! function_exists( 'fictioneer_sql_get_story_chapter_relationship_data' ) )
return $results;
}
}
/**
* Returns comment count of a story
*
* @since 5.26.0
*
* @global wpdb $wpdb WordPress database object.
*
* @param int $post_id Post ID.
*
* @return int Comment count.
*/
function fictioneer_sql_get_comment_count( $post_id ) {
global $wpdb;
$query = $wpdb->prepare(
"SELECT comment_count
FROM {$wpdb->posts}
WHERE ID = %d",
$post_id
);
return $wpdb->get_var( $query );
}
/**
* Updates the comment count of a post
*
* @since 5.26.0
*
* @global wpdb $wpdb WordPress database object.
*
* @param int $post_id Post ID.
* @param int $count Comment count.
*/
function fictioneer_sql_update_comment_count( $post_id, $count ) {
global $wpdb;
$wpdb->update(
$wpdb->posts,
array( 'comment_count' => $count ),
array( 'ID' => $post_id ),
['%d'],
['%d']
);
}

View File

@ -425,6 +425,8 @@ function fictioneer_increment_story_comment_count( $comment_id ) {
if ( $story_data ) {
$story_data['comment_count'] = intval( $story_data['comment_count'] ) + 1;
update_post_meta( $story_id, 'fictioneer_story_data_collection', $story_data );
fictioneer_sql_update_comment_count( $story_id, $story_data['comment_count'] );
}
}
@ -451,6 +453,8 @@ function fictioneer_decrement_story_comment_count( $comment_id ) {
if ( $story_data ) {
$story_data['comment_count'] = max( 0, intval( $story_data['comment_count'] ) - 1 );
update_post_meta( $story_id, 'fictioneer_story_data_collection', $story_data );
fictioneer_sql_update_comment_count( $story_id, $story_data['comment_count'] );
}
}

View File

@ -429,6 +429,9 @@ if ( ! function_exists( 'fictioneer_get_story_data' ) ) {
$meta_cache['comment_count'] = $comment_count;
$meta_cache['comment_count_timestamp'] = time();
// Update post database comment count
fictioneer_sql_update_comment_count( $story_id, $comment_count );
// Update meta cache and purge
update_post_meta( $story_id, 'fictioneer_story_data_collection', $meta_cache );
@ -579,6 +582,9 @@ if ( ! function_exists( 'fictioneer_get_story_data' ) ) {
// Update story total word count
update_post_meta( $story_id, 'fictioneer_story_total_word_count', $word_count );
// Update post database comment count
fictioneer_sql_update_comment_count( $story_id, $comment_count );
// Done
return $result;
}