如何通过wp_commentdata中存储的meta_值过滤Wordpress中的注释?

如何通过wp_commentdata中存储的meta_值过滤Wordpress中的注释?,wordpress,Wordpress,我能够通过评论表单中的自定义字段,在Wordpress数据库的wp_commentmeta表中输入以下数据: meta_id | comment_id | meta_key | meta_value -------------------------------------------------- 9 | 6 | commentoptions | option1 10 | 7 | commentoptions | optio

我能够通过评论表单中的自定义字段,在Wordpress数据库的wp_commentmeta表中输入以下数据:

meta_id | comment_id | meta_key       | meta_value
--------------------------------------------------
9       | 6          | commentoptions | option1
10      | 7          | commentoptions | option2
现在,我正试图找到一种方法,根据meta_值筛选注释,以便在一个页面上的两列中显示它们,左一列,右一列。我的意思是:第一条评论必须放在左边的一栏,下一条评论必须放在右边的一栏

起初,我认为“我很幸运,只需要创建两个div,用CSS定位它们,并用wp_list_comments函数过滤评论”,但是。。。wp_list_comments函数无法执行此操作


有人能告诉我怎么做吗?提前谢谢

编辑:有点晚了。。。我刚刚意识到我最初的答案是针对post元数据的。我将在下面留下我的原始答案,因为它可能有助于获取帖子的元字段。在顶部,您将找到获取注释元字段的方法

假设这些元数据通过wordpress API进入数据库,下面的代码将帮助您为特定的评论文章提取元数据

订正答复:

<?php 
$comment_id = 123;
$key = "commentoptions"; // change to whatever key you are using
$single = true; // whether or not you want just one or multiple values returned associated with the same key, see comments below about use

$meta_values = get_comment_meta($comment_id, $key, $single); 
?>


原始答案(适用于获取帖子元字段)


您也可以使用如下所示的get_post_custom_values(),如果每个键都有多个值,这是一种更好的方法。如果使用$single=false,上面的代码也会这样做,但由于get_post_meta()函数的工作方式,因此不推荐使用该函数-如果只返回一个值,它将给出实际的字符串值,否则它将返回一个值数组。如果一个键有一个或多个值,下面的方法将返回一个数组,因此它将生成更干净、更直观的代码

<?php
$post_id = 123;
$key = "commentoptions";

$meta_values = get_post_custom_values($key, $post_id);
?> 


非常感谢您的帮助,特别是因为已经很晚了!我可以从数据库中检索数据(meta_值),但是。。。我不知道如何根据这个meta_值在两列中列出注释。一开始我想的是这样的:“wp_list_注释(数组('meta_key'=>'commentoptions','meta_value'=>'option1'));”但是wp_list_注释没有提供这一点。任何建议都将不胜感激。@Morris一种方法是在WP中使用get_comments()函数。这将为您提供所需评论的数据(您可以指定要检索的帖子或评论ID)。结果是一个包含所有注释数据的数组。
<?php
$post_id = 123;
$key = "commentoptions";

$meta_values = get_post_custom_values($key, $post_id);
?>