WordPress Comments Feed for a Custom Post Type

We have already covered how to include custom posts in WordPress feeds. The changes made affect the comments feed and now will list comments for all post types. To list all comments for a specific post type we can use the post_type parameter and a more complicated function by taking advantage of the WordPress filter comment_feed_where:

function custom_comment_feed_where($where) {
	global $wpdb;
	// get the post type
	$post_type = $_GET["post_type"]; //get_query_var('post_type');
	if ( ! empty( $post_type ) ) {
		$post_type = sanitize_user($post_type, true);
		$where .= " AND $wpdb->posts.post_type = '$post_type'";
	}
	return $where;
}
add_filter('comment_feed_where', 'custom_comment_feed_where');

We can then load the comments for a specific post type using the post_type parameter in the URL:

Comments on an Author’s Posts

In reply to Stéfan’s comment below it is possible to use the previous method to show a feed of comments for an author’s posts only.

We use a new parameter in the query string called post_author which is set to the slug of the user/author. If there is a value present we load the user from the database and then retrieve the user ID. The ID is then used to filter the comments.

function custom_comment_feed_where($where) {
	global $wpdb;			

	// post type code could be included here

	// get the author if there is one
	$post_author = $_GET["post_author"];
	if ( ! empty( $post_author ) ) {
		$post_author = sanitize_user($post_author, true);
		// set default author id
		$author_id = 0;
		// load the author from slug to get the id
		$author = get_user_by('slug', $post_author);
		if ( $author ) {
			$author_id = $author->ID;
		}
		// filter posts by the post author
		$where .= " AND ($wpdb->posts.post_author = " . absint($author_id) . ')';
	}
	return $where;
}
add_filter('comment_feed_where', 'custom_comment_feed_where');

We then load the comments for an author’s posts using the post_author parameter in the feed URL:

Using post_type we can show comments for an author’s different post types:

 

 

This entry was posted in Programming and tagged . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

3 Comments

  1. Posted April 26, 2011 at 9:53 am | Permalink

    Interesting hack. I’m looking for something that sounds similar (maybe it’s not, I don’t have any skills in programming…).

    What I’m looking for is a way to get a feed of all comments made on all posts from a specific user.
    The objective is to allow each author of a multi-author blog to be able to keep track of comments on his posts.

    • Posted May 6, 2011 at 4:21 pm | Permalink

      Stéfan I have posted an update above that I think meets your requirements.

  2. Posted January 23, 2016 at 2:46 pm | Permalink

    Thanks for your nice trick for feed comments. Please leave me message if you are also interested for any cooperation.

Post a Comment

*
*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

* = Required