WordPress-将标签分类添加到注释

WordPress-将标签分类添加到注释,wordpress,tags,comments,Wordpress,Tags,Comments,我正在做一个项目,需要注释有标签,并且可以通过标签进行搜索。有没有一种方法可以在WP中实现它,或者我应该寻找一些解决方法(比如创建子帖子类型而不是评论,并对其应用标记) 如果有,我怎么做 谢谢。您可以使用评论元来存储和检索特定评论的标记 首先,将标记字段添加到注释表单并填充标记。下面的代码将在comment textarea之后立即添加一个“select”字段,并用标记填充它 add_filter( 'comment_form_defaults', 'change_comment_form_de

我正在做一个项目,需要注释有标签,并且可以通过标签进行搜索。有没有一种方法可以在WP中实现它,或者我应该寻找一些解决方法(比如创建子帖子类型而不是评论,并对其应用标记)

如果有,我怎么做


谢谢。

您可以使用评论元来存储和检索特定评论的标记

首先,将标记字段添加到注释表单并填充标记。下面的代码将在comment textarea之后立即添加一个“select”字段,并用标记填充它

add_filter( 'comment_form_defaults', 'change_comment_form_defaults');
function change_comment_form_defaults( $default ) {
    $commenter = wp_get_current_commenter();
    $out = '<label for="comment_tags">Tags:</label><select name="comment_tags" multiple>';
    foreach ( get_tags() as $tag ) {
        $out .= '<option value="<?php echo $tag->term_id; ?>"><?php echo $tag->name; ?></option>';
    }
    $out .= '</select>';
    $default[ 'comment_field' ] .= $out;
    return $default;
}
我宁愿将每个标记存储为单独的记录,而不是将选定的标记存储为单个记录中的数组。这将使基于标签搜索评论变得更容易

$tags = array(1,32,5,4); /* Replace it with tags you want to search */
$args = array(
    'meta_query' => array(
        array(
            'key' => 'comment_tag',
            'value' => $tags,
            'compare' => 'IN'
        )
    )
 );
$comment_query = new WP_Comment_Query( $args );
当您想要检索注释的标记时,您可以获取\u comment\u meta

使用WP_Comment_Query根据标记搜索注释

$tags = array(1,32,5,4); /* Replace it with tags you want to search */
$args = array(
    'meta_query' => array(
        array(
            'key' => 'comment_tag',
            'value' => $tags,
            'compare' => 'IN'
        )
    )
 );
$comment_query = new WP_Comment_Query( $args );
希望这对你有帮助

$tags = array(1,32,5,4); /* Replace it with tags you want to search */
$args = array(
    'meta_query' => array(
        array(
            'key' => 'comment_tag',
            'value' => $tags,
            'compare' => 'IN'
        )
    )
 );
$comment_query = new WP_Comment_Query( $args );