Show comments of private posts in the comments widget

In September 2017 I wrote a blog post on how to show tags from private posts in the tags widget. Three weeks ago I got a comment (on the German blog post) if something similar would be possible for comments from private posts. In this posts I would like to present you a simple solution to that comment.

Filter the comments query arguments

By default the comments widget will only show five latest approved comments from public posts. This is how the query arguments look like:

array (
  'number' => 5,
  'status' => 'approve',
  'post_status' => 'publish',

As almost always with WordPress, there is a filter to change those default values and fortunately we can use such a filter:

function comments_of_private_posts( $args ) {
	if ( current_user_can( 'read_private_posts' ) ) {
		if ( ! is_array( $args['post_status'] ) ) {
			$args['post_status'] = array( $args['post_status'] );
		$args['post_status'][] = 'private';

	return $args;
add_filter( 'widget_comments_args', 'comments_of_private_posts' );

This code snippet will first check, if the current user has the capabilities to read private posts (you may want to skip this check, if you even want to show comments from private posts to all users). Then we convert the post_status index of the arguments array from a string to an array and add private to this array. Now any user that can read posts should also see comments to those posts in the comments widget.


I’ve never thought about private posts and the comments widget, so thanks to Irit for her question in the comment. It gave me an idea for a new blog post and fortunately the solution was quite simple. If you want to use this function yourself, you can find the code as a plugin in a GIST. So simply download it as a ZIP file and install it as a plugin on your site.

You might not need the same on your blog, but you can use the same filter to change the number of comments in the widget or to change some other arguments.

If you have any questions to one of my blog posts, please don’t hesitate to ask. It might end up in a new blog post and can help others as well.

Posted by

Bernhard is a full time web developer who likes to write WordPress plugins in his free time and is an active member of the WP Meetups in Berlin and Potsdam.

Leave a Reply

Your email address will not be published. Required fields are marked *