How to retrieve comments from WordPress posts in a given taxonomy?

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

Read More

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

Related posts

Leave a Reply

2 comments

  1. I think the main problem is you are using post_type instead of type 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 the post__in option…

    $tax_id = get_queried_object_id(); // current taxonomy ID number
    $taxonomy_array = get_posts( array(
        'fields' => 'ids',
        'post_type' => 'movies',
        'tax_query' => array(
            array(
                'taxonomy' => 'studio',
                'terms' => $tax_id
            )
        )
    ) );
    
    $args = array(
        'post__in' => $taxonomy_array,
        'order' => 'DESC',
        'type' => 'comment',
        'status' => 'approve',
        'parent' => 0
    );
                            
    $comments_query = new WP_Comment_Query;
    $comments = $comments_query->query( $args );