0

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!

0

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;
    }
);
1
  • Thank you so much for this, where abouts would I make these edits? Also if I was to want this to work on pages with comments enabled instead would I switch out 'post_id' with 'page_id'? I'll take a look at it and get back to you, thank you – Fletch Armstrong Aug 27 '20 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.