Pippins Plugins
  • Email
  • Facebook
  • Feedburner
  • Github
  • Google
  • Twitter
  • Vimeo
  • Youtube
  • Rss
  • About
  • News
  • Join the Site
    • Member Benefits
    • Member Plugins
    • Email Notifications
  • Plugin Store
    • Affiliate Area
    • Checkout
  • Plugins
    • Plugin Portfolio
      • Plugin Portfolio – List View
    • Free
    • Premium
    • Member Plugins
    • Coding Standards
    • Get Plugin Support
  • Tutorials
    • Series
      • Plugin Development 101
      • Creating a User Follow System Plugin
      • Customizing Restrict Content Pro
      • Displaying Content with Easy Content Types
      • Writing Your First WordPress Plugins, Basic to Advanced
      • Working with Widgets
      • User Submitted Image Galleries
      • Plugin Thoughts
      • Integrating Stripe.com with WordPress
      • WordPress Rewrite API
    • Member Exclusive
      • Free Members
      • Subscriber Only
    • Difficulty
      • Beginner
      • Intermediate
      • Advanced
    • Action and Filter Hooks
    • Ajax
    • Custom Post Types
    • External APIs
    • Short Codes
    • Taxonomies
    • Video Tutorials
    • Widget Tutorials
    • WordPress Admin / Dashboard
    • Working with jQuery
    • WordPress Database
    • Writing Plugins
    • Tag Index
  • Reviews
  • Support Forum
  • Contact
    • Support the Site
    • Request Code Review
    • Plugin Support

Querying Comments with WP_Comment_Query and Meta Query in 3.5

Posted on December 14, 2012 by Pippin in Advanced, Tutorials 11 Comments
Home» Tutorials » Advanced » Querying Comments with WP_Comment_Query and Meta Query in 3.5
Tweet
Love It - 1

WordPress 3.5 has introduced meta queries for the WP_Comment_Query class, which allows us to retrieve comments from the database that have specific meta values and/or keys attached to theme, just like we have been able to do with posts since 3.2.

It is a pretty straight forward process. We simply pass an array to the meta_query argument with the keys / values we want to query, like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$args = array(
	'meta_query' => array(
		array(
			'key'   => 'your_meta_key',
			'value' => 'meta_value'
		)
	)
);
 
$comments_query = new WP_Comment_Query;
$comments       = $comments_query->query( $args );
 
if( $comments ) :
	foreach( $comments as $comment ) :
		// do stuff with comments here
	endforeach;
endif;

You can also query multiple meta values/keys, like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$args = array(
	'meta_query' => array(
		array(
			'key'   => 'your_meta_key',
			'value' => 'meta_value'
		),
		array(
			'key'   => 'second_key',
			'value' => 'value_2'
		)
	)
);
 
$comments_query = new WP_Comment_Query;
$comments       = $comments_query->query( $args );
 
if( $comments ) :
	foreach( $comments as $comment ) :
		// do stuff with comments here
	endforeach;
endif;

When passing multiple keys/values, WordPress will assume an AND relation for the meta fields, meaning that only comments with both meta keys and values will be returned. You can change this, however, to an OR relation so that comments with one or both meta keys/values are returned:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$args = array(
	'meta_query' => array(
		'relation'  => 'OR',
		array(
			'key'   => 'your_meta_key',
			'value' => 'meta_value'
		),
		array(
			'key'   => 'second_key',
			'value' => 'value_2'
		)
	)
);
 
$comments_query = new WP_Comment_Query;
$comments       = $comments_query->query( $args );
 
if( $comments ) :
	foreach( $comments as $comment ) :
		// do stuff with comments here
	endforeach;
endif;

If you have never worked with comment meta before, here are some related functions you will find useful:

  • add_comment_meta()
  • update_comment_meta()
  • delete_comment_meta()
  • get_comments()
Tweet Follow @pippinsplugins
add_comment_meta, delete_comment_meta, get_comments, meta_query, update_comment_meta, wp_comment_query

11 comments on “Querying Comments with WP_Comment_Query and Meta Query in 3.5”

  1. Chris Coyier says:
    December 15, 2012 at 11:34 am

    Would be useful in combination with plugins like Feature Comments (http://wordpress.org/extend/plugins/feature-comments/), ilke I use on CSS-Tricks to highlight good comments. I could make a widget (or whatever) to display the most recent featured comments.

    Reply
    • Pippin says:
      December 15, 2012 at 11:35 am

      That’s a great idea. How do you currently mark comments as featured? In the admin or the front end?

    • Chris Coyier says:
      December 16, 2012 at 12:53 pm

      Both! It injects links into the comment threads so you can click from there. Or, you can mark it Ajax-y style from the admin area where you mark stuff as spam/delete/edit/etc, AND you can use radio buttons to mark them from the screen where you edit comments in the full-on edit-mode.

  2. Jared says:
    December 20, 2012 at 1:02 am

    Alright, I have a question. I’m trying to use your featured comment plugin and then sort by featured comments so those show at the top.

    So how do I apply this to wp_list_comments? It accepts $comments, which is the result of the normal get_comments() query. So in your example would I execute that query before wp_list_comments and then pass the final $comments through wp_list_comments?

    Reply
    • Pippin says:
      December 20, 2012 at 4:27 pm

      I would suggest you use the pre_get_comments hook, which works just like pre_get_posts, to apply your meta query before wp_list_comments() is called.

    • Jared says:
      December 21, 2012 at 1:26 am

      Alright, this is way more complex than I original thought.

      The query args don’t work right because when you set

      'orderby' => 'meta_value',
      'meta_key' => 'featured',

      or similar, it *only* grabs the comments that are marked featured.

      Additionally it seems the pre_get_comments only doesn’t fire on wp_list_comments. It does when you use get_comments() (which runs a straight query like you have in your code) but that isn’t required to pass to wp_list_comments but is optional.

      I’m starting to think giving the current limitations and dealing with threading what I originally set out to do isn’t possible.

    • Pippin says:
      December 21, 2012 at 7:31 am

      Oh dang, I hadn’t thought about that. Maybe an idea would be to actually do two comment queries, one for featured and then one (normal) for all others. That way you’d end up with two comment loops and they’d be stacked on top of each other. You could easily exclude featured comments from showing up in the bottom set.

    • Jared says:
      December 22, 2012 at 12:28 pm

      Yeah so I actually tried that when I was messing with everything. For top level comments that works, however replies are all screwy. If you reply to a top level featured comment, the reply does not inherit the featured status, so when its posted it shows with the “normal” comments below.

      I’m not sure how, but basically there needs to be a check when a comment is posted, that if the parent is featured then make it featured as well.

    • Pippin says:
      December 23, 2012 at 10:37 am

      Ah, that’s an interesting problem. I can see a couple of possible solutions:

      Add a helper function to get comment IDs for all replies of featured comments so that these can be passed to an exclude parameter
      Apply the “featured” status to child (reply) comments when marking a parent as featured. This would also include marking a child as featured if it is left on an already-featured comment

      Thoughts?

    • Jared says:
      December 23, 2012 at 10:44 am

      I think there are two parts that would need to be done.

      The first would deal with featuring/unfeaturing comments. It would need to perform the action on all child comments regardless of depth. So if I mark a comment featured that has 3 levels, all of them get marked featured.

      The second thing would be for making replies after a comment has already been marked. I’m not sure where this would be hooked, but basically on comment post it needs to get the parent comment ID and check to see if it is featured, and if it is then inherit that.

      Needless to say at first glance I didn’t think this would be that big of a deal, but then I forgot how screwy the comments are :P

    • Pippin says:
      December 27, 2012 at 6:35 pm

      This has definitely turned into a bit of a rabbit hole :D

      I’m going to do some updates to the plugin next week so I’ll see if I can work some of this in.

Leave a Reply Cancel reply

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

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=""> <strike> <strong>

  • Login

Lost your password?

Please enter your username or e-mail address. You will receive a new password via e-mail.

  • Facebook Become a Fan Like

  • Twitter Subscribe on Twitter Follow

  • YouTube Follow my Videos Subscribe

  • RSS Feed Subscribe with RSS Subscribe

Easy Digital Downloads

Most Loved

  • Love It Pro for WordPress
  • Write a “Love It” Plugin with Ajax to Let Users Love Their Favorite Posts / Pages
  • Simple Notices Pro Plugin for WordPress
  • User Bookmarks for WordPress
  • Front End Registration and Login Forms Plugin

Similar Plugins and Posts

Sorry, no related items found.

Latest Premium Content

  • Plugin Development 101 – Introduction to Adding Dashboard Menus
  • Plugin Development 101 – Intro to Loading Scripts and Styles
  • User Follow System – Part 5
  • Plugin Development 101 – Intro to Short Codes
  • Plugin Development 101 – Registering a Custom Post Type
  • Plugin Development 101 – Intro to Actions

Latest Tutorials

  • Test Your Plugins with RTL (0)

    Right-To-Left languages are those that...

  • Submitting Your First Pull Request to a WordPress Plugin on Github (5)

    Github is an extremely popular tool for managing WordPress plugins, and one...

  • Plugin Development 101 – Introduction to Adding Dashboard Menus (1)

    Adding new menus, both top level and sub level, to the WordPress Dashboard is a really common task for plugins...

Enter your email to receive automated updates when new posts are published

Latest Tweets

  • @HipHopMakers should be back shorlty
    May 25, 2013
  • @mrpritchett good idea. Not in the plugin currently but I like the odea
    May 25, 2013
  • @mrpritchett what kind of short codes?
    May 25, 2013

Topics

hook meta box Rémi Corson featured shortcodes campaign monitor add_options_page register_setting Sugar Event Calendar attachments add_shortcode wp_enqueue_script the_content image forms short codes Related posts login do_action authors mail chimp attachment plugin recent posts comments post types bbpress apply_filters short code taxonomies custom post type Ajax images gallery Stripe jquery taxonomy users widgets add_filter easy content types add_action widget restrict content pro easy digital downloads

Weekly Newsletter

Useful Links

  • Join the Site
  • Plugin Store
  • Affiliate Area
  • Tag Index
  • Support the Site
  • Suggest a Tutorial
  • Random Post
  • Contact

Monthly Archives

(c) 2013 Pippin's Plugins