Question

I'm looking to basically have a unified comment section, so if a user posts on page a, it also shows on page b.

I've searched online but have been unable to find any documentation on this and wondered if it is easy to achieve as I'm totally new to wordpress coding etc but feel it can't be too difficult to pull up the same comment section on every page but I could be wrong!

Was it helpful?

Solution

With some testing this appears to be achievable with two filters.

Firstly you want to set front end queries for comments to query for comments for all posts, instead of just the current post. That can be done like this:

add_action(
    'pre_get_comments',
    function( $comment_query ) {
        if ( ! is_admin() ) {
            $comment_query->query_vars['post_id'] = 0;
        }
    }
);

However some logic in comment templates will be based on the number of comments that the WordPress thinks the post has. This is stored on each post as comment_count separate to the comment query for performance reasons, so just changing which comments are queried won't affect this number. However, with the get_comments_number filter we can replace each post's comments count with the total number of comments on the site, like this:

add_filter(
    'get_comments_number',
    function( $count ) {
        if ( ! is_admin() ) {
            $count = wp_count_comments()->approved;
        }

        return $count;
    }
);
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top