0

I have introduced a custom field in comments form using add_comment_meta() function.

I would like to know if there is a way to edit the data of the comment meta just before the post data is saved so that every time the data is replaced with my value before it is saved.

Lets say that the key is brand_id.

I have used the following function to edit the custom_meta Value

function update_comments_meta($post_id) {
    $args = array(
        'post_id' => $post_id,
    );
    $comments = get_comments($args);
    if (!empty($comments)) {
        foreach ($comments as $comment) {
            echo $comment;
            update_comment_meta($comment->comment_ID, 'brand_id', 'hard_coder');
        }
    }
}

add_action('save_post', 'update_comments_meta');
1
  • You want to update all brand_id comment meta values, related to a given post, each time that post is updated?
    – birgire
    Aug 25, 2015 at 11:21

1 Answer 1

1

You can use "save_post" action-hook

Add the code below into functions.php and enchance with your comment_meta code.

function update_comments_meta( $post_id ) {
   // Do whatever add/update_comment_meta code you need
}
add_action( 'save_post', 'update_comments_meta' );

UPDATE.

As an example i've attach code below. It performs on post save/update action fired. Code gets current post comments (all) then looping through received comment object it launch update_comment_meta with ID extracted from comments object.

function update_comments_meta($post_id) {
    $args = array(
        'post_id' => $post_id,
    );
    $comments = get_comments($args);
    if (!empty($comments))
        foreach ($comments as $comment) {
            update_comment_meta($comment->comment_ID, 'YOUR_META_KEY', 'YOUR_META_VALUE');
        }
}

add_action('save_post', 'update_comments_meta');

Hope this is the case that you where interested in. Code tested on 4.3 twentyfifteen.

Refferences:

  1. update_comment_meta()
  2. get_comments()
7
  • how can i use it to modify a post variable that is a custom field brand_id Aug 25, 2015 at 20:11
  • I have updated accordingto your code ....please check it dosent work Aug 25, 2015 at 20:26
  • I've updated answer with an example of code. Your update_comment_meta receive wrong input data (You pass post_ID instead comment_ID). Additionally: In your question you specify that you're searching for a way how to update "comment meta just before the post data is saved". If you want to update specific comment, just add update_comment_meta('SPECIFIC_COMMENT_ID', 'YOUR_META_KEY', 'YOUR_META_VALUE'); to the code posted in original answer.
    – Ignat B.
    Aug 25, 2015 at 21:21
  • thanks for your update ....but i am sorry to say that it is not working yet. Second this is that i tried to check it manually and went to this website codex.wordpress.org/Function_Reference/get_comments to find that there is no field as comment_ID Aug 26, 2015 at 11:14
  • How about using codex.wordpress.org/Plugin_API/Filter_Reference/… for comment meta? Aug 26, 2015 at 11:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.