Frontend post + COMMENT with custom fields
-
Hello,
I need to create a post and the first comment with metadata from the frontend.
It is a review website where people can add a product generic info sheet (product post type), and their multi-criteria rating (comment).I am using ACF and ACF Frontend Form for Elementor.
I created two ACF fields groups:
– one for the ratings / comments
– one to create a new product AND the very first rating.I managed to create new posts, comments alone work fine,
but I cannot add my ACF fields to the first comment when the post is created.I tried this code in functions.php:
function my_acf_comment_meta ( $post_id ) { $meta1 = get_field('form_meta_1'); $current_user = wp_get_current_user(); $agent = $_SERVER['HTTP_USER_AGENT']; $ip = $_SERVER['REMOTE_ADDR']; $com_meta = array( "comment_meta_1" => $meta1, "comment_meta_2" => 'Lorem ipsum', ); $data = array( 'comment_post_ID' => $post_id, 'comment_author' => $current_user->display_name, 'comment_author_email' => $current_user->user_email, 'comment_content' => $comment, 'comment_agent' => $agent, 'comment_author_IP' => $ip, 'comment_date' => date('Y-m-d H:i:s'), 'comment_date_gmt' => date('Y-m-d H:i:s'), 'comment_approved' => 1, 'comment_meta' => $com_meta, ); $comment_id = wp_insert_comment($data); } add_action('acf/save_post', 'my_acf_comment_meta', 99);
This way I can add a comment with only the static value of
comment_meta_2
,
butcomment_meta_1
is empty, instead of showing the value of my frontend form fieldform_meta_1
.If don’t push my fields in the comment array but I try
$meta1 = get_field('form_meta_1'); add_comment_meta($comment_id, 'comment_meta_1', $meta1);
add_comment_meta($comment_id, ‘comment_meta_2’, ‘Lorem ipsum’);`
I get the very same result: static data is stored in the comment, ACF data is not.
Any ideas? Thank you in advance
- The topic ‘Frontend post + COMMENT with custom fields’ is closed to new replies.