I am trying to write some code in my plugin which displays only comments for posts which have a specific value set for a custom taxonomy. My set up is:
Custom post type – Object
Custom taxonomy – Sources
Example value – ABC Museum
Commenting is enabled for Objects
I can retrieve comments by user and by each custom post
I have tried:
$meta_query = array('key' => 'sources', 'value' => 'ABC Museum');
$args = array(
'number' => 5,
'post_type' => 'Object',
'meta_query' => array($meta_query)
);
$comments_query = new WP_Comment_Query;
$comments = $comments_query->query( $args );
but this returns an empty array. Is this just a silly syntax error on my part, or have I misunderstood the use of meta_query and it won’t work for custom taxonomies and custom post types?
Looking at http://pippinsplugins.com/querying-comments-with-wp_comment_query-and-meta-query-in-3-5/ I think it may be the latter – the meta has to be related to the comment, not teh post that it was added to, is that correct? Unfortunately http://codex.wordpress.org/Function_Reference/get_comments is rather thin on details and examples!
Thanks
The meta_key value needs to be attached to comment itself Check Here. WP Comment Query will return nothing if you pull comments based on meta value of their respective posts.
I think the main problem is you are using
post_type
instead oftype
which is the difference between a comment query and a normal post query in WordPress. If you want to have a previous array to filter out which types of posts (or custom posts) to pull from, that needs to be e.g. $taxonomy_array which you can associate with thepost__in
option…