[wp-hackers] Separate Comments of Custom Post Type

Paul paul at codehooligans.com
Sat Aug 7 18:30:09 UTC 2010


The following is just my opinion on a solution and may not be considered the best method at the time for adding your own comments menu option. I'm using a plane vanilla WP 3.0.1 setup. I have a custom post type 'products' registered. 

From what I can see this is easy or hard depending on your level of coding ability. 

Let's start with the first task. I'm going to use a project I'm currently working on as an example. In my project I've created a new post type 'product'. This is a simple eCommerce site. When you register a post type WP provides the default shell left nav section on wp-admin. This is the normal list products and add new products options. In order to add a new menu item to your custom post type section you need to hook into the 'admin_menu' action like the following

add_action( 'admin_menu', array(&$this,'add_product_subnav'));
function add_product_subnav()
{
	add_submenu_page( 'edit.php?post_type=product', 'Comments', 'Comments', 'manage_options', 
			'product_comments_menu', array(&$this, 'product_comments_menu') );		
}

function product_comments_menu()
{
	// Add code here to display the comments for your post type. 
}

Note the first argument of the add_submenu_page function. This post_type must match your new registered post type. For the add action I added this to the very bottom of my init function just after the call to register_post_type().

So at this point you have a new nav item on your post type menu. The nav item when clicked will call 'product_comments_menu function'. Here is where things get sort of ugly. As far as I can tell there is no easy way to filter the comments by post_type. For example I would expect there to be some method to filter the WHERE clause of the comments query. Nothing. Take a look at the /wp-admin/edit-comments.php code. you will basically want to copy most of this code into your version of 'product_comments_menu' function. 

In edit-comments.php around line 274 you will see the call to '_wp_get_comment_list'. This is where the list of comments is pulled in. I would add a foreach loop just after this and remove any items not matching your post type. Since the comment item structure contains the post_type value this should be pretty simple. Something like:

foreach($_comments as $idx => $comment)
{
	if ($comment=>post_type != "YOUR POST TYPE")
		unset($comments[$idx];
}

After the foreach $_comments should be just your post type comments. Again copy from the edit-comments.php code lines 386-411 to display the table. This should get your started though have not gone as far as editing a comment to see if things work correctly. 

Good luck. 

P-





> On 7 August 2010 12:32, Baki Goxhaj <banago at gmail.com> wrote:
> 
>> Hi Fellow Hackers,
>> 
>> I'm trying to have a custom post type that will have comments, but I want
>> those comments to be separate form default comments and accessed through
>> another menu item. Is that possible? if yes, is there something I can look
>> at?
>> 
>> Kindly,
>> 
>> Baki Goxhaj
>> www.wplancer.com | www.banago.info | www.lintuts.com
>> 
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers



More information about the wp-hackers mailing list