带钥匙'comment_post_ID'
是您的评论将显示的位置,因此需要产品 ID
然后你可以使用update_comment_meta()专门的 WordPress 功能来添加评级,例如:
update_comment_meta( $comment_id, 'rating', 3 ); // The rating is an integer from 1 to 5
所以你的代码会像(where $product_id
是本次评论的目标产品 ID):
$comment_id = wp_insert_comment( array(
'comment_post_ID' => 37, // <=== The product ID where the review will show up
'comment_author' => 'LoicTheAztec',
'comment_author_email' => '[email protected]', // <== Important
'comment_author_url' => '',
'comment_content' => 'content here',
'comment_type' => '',
'comment_parent' => 0,
'user_id' => 5, // <== Important
'comment_author_IP' => '',
'comment_agent' => '',
'comment_date' => date('Y-m-d H:i:s'),
'comment_approved' => 1,
) );
// HERE inserting the rating (an integer from 1 to 5)
update_comment_meta( $comment_id, 'rating', 3 );
经过测试并按预期工作。
The 作者电子邮件和user ID需要是一些现有的。