[wp-hackers] Separate Comments of Custom Post Type
Paul
paul at codehooligans.com
Sat Aug 7 20:12:01 UTC 2010
Freakin Awesome Mike!
P-
On Aug 7, 2010, at 3:40 PM, Mike Schinkel wrote:
> On Aug 7, 2010, at 2:30 PM, Paul wrote:
>> 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.
>
> Actually, the following works (and the code below is packaged as a plugin here: http://gist.github.com/513084):
>
> add_action('query', 'hook_to_filter_comment_admin_by_post_type');
> function hook_to_filter_comment_admin_by_post_type($sql) {
> global $pagenow;
> if ($pagenow=='edit-comments.php' &&
> !empty($_GET['post_type']) &&
> post_type_exists($_GET['post_type']) &&
> preg_match('#^SELECT \* FROM wp_comments c#',$sql)) {
> $sql = str_replace(' WHERE '," WHERE p.post_type='{$_GET['post_type']}' AND ",$sql);
> }
> return $sql;
> }
>
> Of course modifying the $sql works in most cases but occasionally it conflicts with another hook so while using the above in the short term he could create a trac ticket to lobby for adding post_type filtering into edit-comments.php or at the very least ask for a filter on the return value of _wp_get_comment_list().
>
> With the above hook Baki can simply pass the "post_type" on the edit-comments.php when setting up his admin menu as you described instead of creating a "product_comments_menu" function like so:
>
> http://example.com/wp-admin/edit-comments.php?post_type=movie
>
> Here's the hook to add a "Comments" menu option to each post type's admin menu:
>
> add_action('admin_menu', 'hook_to_add_comment_admin_menu_option_for_each_post_type');
> function hook_to_add_comment_admin_menu_option_for_each_post_type() {
> $post_types = apply_filters('comment_admin_menu_post_types',get_post_types(array('public'=>true,'show_ui'=>true)));
> foreach($post_types as $post_type) {
> if ($post_type!='post') // Don't do for Posts, they already gota one!
> add_submenu_page("edit.php?post_type={$post_type}",__('Comments'),__('Comments'),'moderate_comments',"edit-comments.php?post_type={$post_type}" );
> }
> }
>
> Note that it adds it's own filter "comment_admin_menu_post_types" so that someone using this can control via the hook in their own theme's function which post types get admin menus for Comments. So if the two post_types you want comments for are "Movie" and "Actor" then this stored in the theme's functions.php file would cause only the menus for those post types to get a Comment option:
>
> add_action('comment_admin_menu_post_types', 'my_comment_admin_menu_post_types');
> function my_comment_admin_menu_post_types($post_types) {
> return array('movie','actor');
> }
>
> Again, here it is as a plugin:
>
> http://gist.github.com/513084
>
> Hope this helps.
>
>
> -Mike
>
> _______________________________________________
> 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