如何在给定的分类法中从WordPress帖子中检索评论


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

我正在尝试在我的插件中编写一些代码,它只显示为自定义分类设置了特定值的帖子的注释。我的设置是:
自定义帖子类型-对象
自定义分类法-来源
示例值-ABC博物馆

已为对象启用注释我可以通过用户和每个自定义帖子检索评论

我试过:

$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 );

但这返回一个空数组。这只是我的一个愚蠢的语法错误,还是我误解了meta_query的使用,它不适用于自定义分类法和自定义帖子类型?

正在查看http://pippinsplugins.com/querying-comments-with-wp_comment_query-and-meta-query-in-3-5/我认为可能是后者——元必须与评论相关,而不是添加到的帖子,这是正确的吗?不幸地http://codex.wordpress.org/Function_Reference/get_comments在细节和例子上相当单薄!

感谢

meta_key值需要附加到注释本身检查此处。若您根据各自帖子的元值提取评论,WP Comment Query将不会返回任何内容。

我认为主要问题是你使用的是post_type而不是type,这是WordPress中评论查询和普通帖子查询的区别。如果您希望有一个以前的数组来筛选要从中提取的帖子类型(或自定义帖子),则需要例如$taxonomy_array,您可以将其与post__in选项关联。。。

$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 );