Home › Forums › Front-end Issues › Frontend: create post and 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
,
but comment_meta_1
is empty, instead of showing the value of my frontend form field form_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
First thing you need to do is install and activate the WordPress Comments Fields. Upon activation, the plugin will add a new menu item labeled ‘Comments Fields’ in your WordPress menu.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.