我正在尝试在wordpress帖子中创建评论列表,以便他们只列出具有某个 meta_key 和 meta_value 的评论。到目前为止,我已经尝试在谷歌上找到答案,并且只有在有人在评论表单中添加评论时才发现如何分配元键/值。
主题的 functions.php
<?php
add_action( 'comment_post', function($comment_id) {
add_comment_meta( $comment_id, $my_meta_key, $my_meta_value );
});
现在我无法弄清楚的是,如何让wordpress帖子过滤评论,以便他们只列出具有我想要的特定元键和值的评论。
我使用下划线制作了我的主题,并使用 wp_list_comments()函数列出了comments.php中的注释。
答案 0 :(得分:2)
以下是如何通过comments_template()
过滤器(适用于WordPress版本4.5 +)在comments_template_query_args
中修改主评论查询的示例:
/**
* Filter non-threaded comments by comment meta, in the (main) comments template
*/
add_filter( 'comments_template_query_args', function( $comment_args )
{
// Only modify non-threaded comments
if( isset( $comment_args['hierarchical'] )
&& 'threaded' === $comment_args['hierarchical']
)
return $comment_args;
// Our modifications
$comment_args['meta_query'] = [
[
'key' => 'some_key',
'value' => 'some_value',
'compare' => '='
]
];
return $comment_args;
} );
根据您的需要调整元查询。请注意,此示例假定 PHP 5.4 + 。